Customizing the SharePoint 2013 Suite Bar Branding using PowerShell

I recently stumbled across a great blog post by Wictor Wilen on how to customize the Suite Bar Branding Element HTML on the Central Administration Web Application in SharePoint 2013. If you’re not familiar with this, we’re basically talking about the area at the top left of the SharePoint 2013 user interface:


By default, it will just say “SharePoint”. While that’s fine for out of the box folks, this can be a great spot for a little customization. It’s also possible to do this on content web applications, not just Central Administration – and I think this could be a good place to provide a description of the web application. It could also serve as a good place for a hyperlink to always allow the user to get back to the root site – sort of like the Portal Site Connection functionality…

While Wictor’s code was great as a starter point, I thought it would be cool to tweak this a little bit by making it a reusable function and also by allowing the HTML to include a hyperlink to the root site collection in the web application. I simply took his code and modified it to make it a little more reusable, and to also allow for the hyperlink functionality. I also included a custom CSS class name on the A tag to allow for easy branding in your custom solutions.

As for how it works, I’ve provided 3 parameters – 2 of which are required. The required parameters are WebAppUrl and Text; meaning you must provide the link to the web application you want to modify and you must provide the text value you want to replace the out of the box value with. Optionally, you can include a third switch parameter for SetTextAsHyperlink. By including this, you’re sipmly telling the PowerShell function to also make the text a hyperlink to the URL of the web application you are modifying.

Here is the PowerShell function:

function Set-SPSuiteBarBrandingElement {
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$webApp = Get-SPWebApplication $WebAppUrl
    $html = "<div class='ms-core-brandingText'><a class='customSuiteLink' href='$WebAppUrl'>"+$Text+"</a></div>"
    $html = "<div class='ms-core-brandingText'>"+$Text+"</div>"
$webApp.SuiteBarBrandingElementHtml = $html

To run it, first dot-source the function and then run it like so – example provided below is for my local VM:

Set-SPSuiteBarBrandingElement -WebAppUrl -Text 'AdventureWorks Intranet'

This should give you something like this:

Optionally, to make the text a hyperlink – run it like so. Notice that when hovering over it’s now a link – pretty cool!

Set-SPSuiteBarBrandingElement -WebAppUrl -Text 'AdventureWorks Intranet' -SetTextAsHyperlink


One last thing I wanted to mention is that in the function above, I’m setting the class of the Anchor tag to “customSuiteLink”. This means if you’re using custom branding, you can reference this CSS class to apply different branding to the link.

Thanks again to Wictor for sharing this!



Add Print Page Functionality Using JavaScript

This morning I received a request from my current client which I’ve gotten before, but not in SharePoint 2010.

They wanted the ability to print page and list view content easily, and without the left and top navigation elements – basically just the main content zone.

I knew there was a way as I had done this in 2007, but I had to do a quick search for the exact JavaScript.  I found a Codeplex project that included a Content Editor Web Part with JavaScript and this worked almost perfectly for my scenario.

This solution works great if you want to print a single page, but I wanted reusability and something that I could do once and have it work for every page in a site.

I ended up copying the script verbatim into a PrintScript.js file which I saved under /_layouts/1033, here is the code of the file:

<!-- Web Part/region to print -->
var WebPartElementID = "ctl00_MSO_ContentDiv";

//Function to print Web Part
function PrintWebPart()
 var bolWebPartFound = false;
 //var currentPage = document.location.href;
 if (document.getElementById != null)
  //Create html to print in new window
  var PrintingHTML = '<HTML>\n<HEAD>\n';
  //Take data from Head Tag
  if (document.getElementsByTagName != null)
   var HeadData= document.getElementsByTagName("HEAD");
   if (HeadData.length > 0)
    PrintingHTML += HeadData[0].innerHTML;
  PrintingHTML += '\n</HEAD>\n<BODY>\n';
  var WebPartData = document.getElementById(WebPartElementID);
  if (WebPartData != null)
   PrintingHTML += WebPartData.innerHTML;
   bolWebPartFound = true;
   bolWebPartFound = false;
   alert ('Cannot Find Web Part');
 PrintingHTML += '\n</BODY>\n</HTML>';
 //Open new window to print
 if (bolWebPartFound)
  var PrintingWindow ="","PrintWebPart", "toolbar,width=800,height=600,scrollbars,resizable,menubar");;
  // Open Print Window

Once I had the JavaScript file saved in the layouts directory, I could simply add a ScriptLink tag in the <head> section of my master page:

<SharePoint:ScriptLink name="PrintScript.js" runat="server"/>

That’s great, now my master page has the ability to print any page; but where do I put the link?  At first I thought of adding a new DIV in the footer area of my page, but I didn’t want the Print link to be included in the printed version of the page.

A quick search on how to add an item to the Site Actions menu gave me EXACTLY what I wanted – and since this is an Intranet solution, every user that needs to print will have at least Contribute permissions and therefore will have the Site Actions menu available to them.

In order to display what I wanted in the Site Actions menu, I added the following code under the <CustomTemplate> section:

<!--Print item added to Site Actions -->
<SharePoint:MenuItemTemplate runat="server" id="MenuItem_PrintPage"
	Text="Print this page"
	Description="This will launch a new window to print this page."
<!--End print item-->

And finally, here is what I’m left with:

And clicking the Print Page option gives me a nice, clean window with no navigational elements:

Configuring Custom 404 Pages using PowerShell

I came across an interesting challenge while working on my current project; which is a migration from a classic HTML-based website to SharePoint 2010 For Internet Sites, Enterprise.  Custom error pages…

Out of the box, SharePoint likes to give the user a useless 404 error like this:


While there is out-of-the-box support for custom error pages in SharePoint 2010, it’s not completely obvious how you set the custom error pages for a web application.  First of all, SharePoint includes an sps404.html page in the /_layouts/{language} folder which by default includes an STSNavigate reference to handle the redirect.  That’s all well and good, but I want my own!

Here’s how I got what I wanted:

Copy the sps404.html page and rename it to sps404custom.html – leave the new file in the same location.  Open the file in your favorite HTML editor and look for the STSNavigate line – change the link to an appropriate page, example /Pages/Page-Not-Found.aspx.

This would replace the OOTB 404 behavior with an automatic redirect to a Publishing page under the root Pages library of my web application.  Cool!

Now I just have to create a page at /Pages/Page-Not-Found.aspx and add some text.  The page will inherit whatever branding elements I’ve applied to the rest of my site, leaving the user with a much cleaner and consistent experience.  Once I’ve done those two things, there’s a little PowerShell that must be done to actually set the custom 404 page:

$WebApp = Get-SPWebApplication http://url
$WebApp.FileNotFoundPage = "sps404custom.html"

Once that’s done, try going to a bogus page in your site and you should be taken to your custom 404 page!


Add Collapsible Help to InfoPath Forms

One of my favorite things to do in relation to SharePoint is business process automation using workflow. What I REALLY enjoy is developing InfoPath forms to go with the workflows.

Recently I was working on an InfoPath form that had a need for a significant amount of text-based help within the form. If it is a sentence or so, it’s not a big deal to put the text inline or even as a screentip, but this was more text than a sentence.

After some thinking and playing with rules and picture buttons, I came up with the solution below:

Help collapsed, notice the [?] icon:

Help expanded, with a different [-] icon:

Prevent theme inheritance on Publishing sub-sites

If you’ve created publishing sites and then tried changing the theme on a sub-site, you’ve probably run into this issue:

You apply the theme and nothing changes!!!!!

For some reason or another, when Publishing features are enabled; sub-sites inherit the theme from their parent site. You can prevent this from happening by doing the following:

Open up the default master page for the site in SharePoint Designer and look for the following tag:

Either delete or comment-out this tag and then save your master page, you should no longer have this issue.