Thursday, April 11, 2019

SharePoint Team site Branding and Master Page Inheritance

It's been over a year since I posted about this topic, but I wanted to share how I'm now currently branding my SharePoint team sites (those that don't have publishing features turned on). This approach will work with 2013/2016 and possibly SharePoint online.

Side note: There's a lot of different approaches to do this. In the past you could use feature stapling. However, the new recommended approach is to use add-ins. My work environment is not currently set up to use SharePoint add-ins. My role is a developer and not an administrator, so I can't even set this up because there is a clear separation of duties. Also, we're in the very slow process of migrating to a new version, so any discussion of having this set up now is out of the question.

Instead of my previous approach, I decided I could script this out in a custom action. What's a custom action? You may recall custom actions in SharePoint designer. You could add these to the ribbon. You could also add custom actions with features and .NET code. That's exactly right! However, I'm referring to using a custom action with a JavaScript file that is referenced in the site or entire site collection and is basically hidden in the site. There's no UI to add the custom action. It can however be added by Powershell

SharePoint PNP powershell commands makes it easy to add these to the site collection.

The idea here is to automatically set the master page as soon as a non-publishing site is created, or to have the option to inherit the look and feel from the site itself. There's no clear way to do either from the UI. With the JavaScript registered as a custom action on the site collection, any sub-site will run the JavaScript. The JavaScript will check to see if the site has just been created. If so, it will automatically update it with it's parent's master page, which you will see instantly change on the screen. Otherwise, it will exit out. If the team site was created a while ago but doesn't have the same branding as it's parent, users can go to Change the Look of the site from the gear icon. There, you will see a new red banner that allows you to inherit the branding of the parent.

In my example, I grab the parent master page because I have a publishing site collection with team sites. You can change this example to explicitly state which master page you want to use.

The PNP commands are simple:

  1. Get the file from here
  2. From the server, open the Powershell Prompt
  3. Connect to the site collection
    1. You may get prompted for credentials
  4. Run the command to upload the javascript file from a folder on the server into the site collection's branding folder
  5. Run the command to register that javascript file as a custom action using the Add-PnPJavaScriptLink pnp command
  6. That's it! Create a team site in your site collection and voila!

$spSiteCollectionUrl = "http://myweb/sites/testsitecollection" 

Write-Host "Connecting to Site..." -foregroundcolor black -backgroundcolor yellow 
Connect-PnPOnline -Url $spSiteCollectionUrl 
Write-Host "Connection Established successfully" -foregroundcolor black -backgroundcolor green 

$Web = Get-PnPWeb 

 UploadFile "\newbranding\scripts\CustomActions\CustomActionTeamSiteBranding.js" "_catalogs/masterpage/NewBranding/scripts/CustomActions"

$customActionJsLink = -join($Web.Url, "/_catalogs/masterpage/NewBranding/scripts/CustomActions/CustomActionTeamSiteBranding.js");
    Write-Host 'Adding custom action link... ' $customActionJsLink -foregroundcolor black -backgroundcolor yellow
    Add-PnPJavaScriptLink -Name CustomActionTeamSiteBranding -Url $customActionJsLink -Scope Site
    Write-Host "Completed adding custom action" -foregroundcolor black -backgroundcolor green

This strategy is part of the overall site provisioning. When I create new site collections, I have a much more involved script that adds site collection admins directly, turns on publishing features, adds all of the branding files (master pages, page layouts, css, etc), creates home pages with default web parts, and adds the custom action javascript files. I hope to share that script on this site very soon!

Monday, March 19, 2018

SharePoint TeamSite Branding and Master Page Inheritance Resolved

UPDATE: Check out this strategy I'm taking now 

Out of the box, creating a team site will not inherit the master page of the site collection.

There's a couple of ways to handle this:

  • Occasionally go to the site collection settings, Look and Feel - >Master Page and selecting, Specify a master page to be used by this site and all sites that inherit from it and check on "Reset all subsites to inherit this site's master page setting"
    • Drawback: This only impacts existing sites, but still doesn't handle any new team sites that are created
  • Going to the team site itself and then manually changing the master page at this location /_layouts/ChangeSiteMasterPage.aspx
    • Drawback: Most end users wouldn't know how to do this and it's not accessible directly from the UI
  • Create a team site, update the master page manually, then save it as a site template
    • Drawback: this will not work if the root parent site is a publishing site or the team site has publishing features turned on
  • Remote provisioning
    • Read more here:
    • Drawback: A little learning curve
I thought about this for a while, and I realized the easiest way is to get some code in a content editor web part onto the teamsite that checks to see if the master page is set, and if not, it sets it. 

I put this in a team site, saved the site as a template, then hid the default team site template from the site collection settings. 

The code is found here:

Monday, March 20, 2017

SharePoint Upload.aspx page freezes when page loads

Recently, I had an issue at a client site where the upload page in any document library would hang a little when opening in IE. There wasn't any issue in Chrome. This issue also wasn't occurring on all workstations, just a few. I knew it had to probably be an Office configuration, but group policy was preventing me from seeing any IE configurations. After diving into the source code of the upload.aspx page, I could see that the page was trying to load an ActiveX control, STSUpld.UploadCtl if the browser version was IE 5 and up. Upon investigation, the different workstations had different versions of this IE Add-on (from IE go to tools->Manage add-ons).

Chrome would skip the loading of the active x object. Therefore, after some head-banging, I decided to update the master page and add the following script to prevent creating the activex object. The script checks that the page is the upload.aspx and if it is, it sets browseris.ie5up to false to continue through the script. It's not a great fix, because it would have to be applied to all the master pages, however this would help appease users until the issue could be resolved by the IT team.

<script type="text/javascript"> 
//added this script because for some users, the upload page would freeze. an activex control on the upload page is loaded for IE5 and up. we set this to be false here so that active x control is not created
if( window.location.href.toLowerCase().indexOf("/_layouts/upload.aspx") >= 0 )
if (typeof browseris !== 'undefined') {
// = false;
browseris.ie5up = false;

Monday, December 19, 2016

Print SharePoint html table

It's been a while since I've posted anything, but I wanted to share a function that will take an html container or div control and print it to a new page. It will also grab the css files and output it to the head of the new page to maintain the styles when you print

This is dependent on the jquery library, found here:
<script type="text/javascript" src=""></script>

//This function will grab the html from the container var to display in a new window.
//It also loads the css files that are on the current page and appends it to the output that is displayed in a new window to maintain the styles
//Title: Specify the title of the page that you wish to name when the print page opens
//Container: pass in the name of the container you wish to print. Ex. $("$tableName")
function PrintPage(title, container)
 //declare the css files with the styles you wish to grab and keep in the in the head of the new window
 var m_siteUrl = _spPageContextInfo.webServerRelativeUrl; //IE11 in sp 2013 does not recognize L_Menu_BaseUrl

 var curDate = new Date();
 var cssLink1 = m_siteUrl + "/siteassets/css/tablesorter/style.css?v=" + curDate.format("MM_dd_yyyy");
 var cssLink2 = m_siteUrl + "/siteassets/css/My_Page_Reports.css?v=" + curDate.format("MM_dd_yyyy");

 var divOutput = $(container).html();

 //append the current date/time to the output window  
 var printDateString = curDate.format("MM/dd/yyyy hh:mm tt");
 printDateString = "<div style='padding-bottom:10px;'>" + printDateString + "</div>";
 divOutput = printDateString + divOutput;
 var cssFile1 = $('<div></div>');
 var cssFile2 = $('<div></div>');

 //used for the callbacks
 var def1 = $.Deferred();
 var def2 = $.Deferred();      
 //loads the css files and grabs all of the css to input into style tags
 var cssFileText = "";
 cssFile1.load( cssLink1, function() { cssFileText += "<style>" + cssFile1.html() + "</style>"; def1.resolve()} );
 cssFile2.load( cssLink2, function() { cssFileText += "<style>" + cssFile2.html() + "</style>"; def2.resolve()} );

 //gets called asynchronously after the css files have been loaded
 $.when(def1, def2).done(function(){

 //generate the html that will be displayed in the new page.
 //set the title of the new window
 //additionally, can put the class names you wish to hide on printing  
   var html = "<HTML>\n" +
    "<Title>" + title + "</Title>\n" +
    cssFileText + "\n" +
     ".hideOnPrint, .rowFilters {display:none}"+
   "</HEAD>\n" +
   "<BODY>\n" + divOutput + "\n" +"</BODY>\n" +
   var printWP ="","printWebPart");;
   //insert content
   //open print dialog

Tuesday, September 24, 2013

Format SharePoint People Picker as Hyperlink on Infopath Display list form

I wanted to display the people picker value in a list form as a hyperlink in SharePoint. By default, it was just a text field on the display form. In a normal SharePoint list form, this field can be modified to disable output escaping so that it formats as an html. I was totally unsure how to accomplish this, and pressed for time, I decided to write a script to format it as a hyperlink. If you know how to do this in InfoPath, please share!

Place this script in a content editor webpart on the display list form:

<script type='text/javascript' src='/js/jquery-1.7.2.min.js'></script>
<script type="text/javascript">

//make sure document is loaded first
$(document).ready(function() { 

function formatUserField()
  var curValue = $(this).text();
  var newhtml = "<a href='javascript:void(0)' onclick='PopUpEmail(\"" + curValue + "\");return false;'>"+ curValue +"</a>";

function PopUpEmail(username)
    var p_recipient = username;
    var p_cc = "";
    var p_subject =  "";
    var p_body =  "";
    var objO = new ActiveXObject('Outlook.Application');     
    var objNS = objO.GetNameSpace('MAPI');     
    var mItm = objO.CreateItem(0);     
    mItm.To = p_recipient;
    mItm.Cc = p_cc;
    mItm.Subject = p_subject;
    mItm.Body = p_body;     
    mItm.GetInspector.WindowState = 2;


Tuesday, September 10, 2013

Embed image in a SharePoint outgoing email using SharePoint Designer workflow activities

If you need to embed an image into a SharePoint outgoing email:

<meta name="content-disposition" content="inline; filename=Myimage.jpg">

  • Format your email, and then to insert the image, create a div tag at the bottom of the email string:
<div style="text-align: center;">
<img src="Myimage.jpg" alt="myimagelogo" />

  • Add the action: Send Email with List Item Attachments
  • Attach the image in the activity, with the same name as used above (Myimage.jpg)
    • In mine, I always want to attach the same image in every outgoing email, so I uploaded an image to a SharePoint document library and I attached the image based on the specific title it contained
  • In the email body of the action, add the workflow variable: emailBody
  • Save, publish and test it out!

Friday, May 3, 2013

Filter SharePoint list with Partial Postback to page

The other day, I wanted to filter a list view web part, triggered from a drop down list on my page. I couldn't connect to a standard out of the box filter web part because the dropdown values were populated with values retrieved from a web service. Every time I select a new value from the drop down, I wanted to automatically filter the sharepoint list with that value, without doing a full postback and refreshing the page (similar to the effect of when you filter a list from the list column options).

To start things off:
  • I created a document library and created a new column called "DocType" with a few choices:
    • Letter
    • Memo
    • Email
  • I made sure the DocType column appeared on my default view (this will be the view I use to do my filtering later)
  • I uploaded a few documents into my library, and provided the DocType for each
  • On my site page, I added the list view web part for this Document Library
  • From the AJAX Options under the list view web part settings:
    • Check off Enable Asynchronous Update
    • Show Manual Refresh Button
  • Using IE developer tools, find the element for the refresh icon with the id of ManualRefresh

Copy the outer anchor tag's onclick event (this event triggers a partial post back to the page)

  • Add a form web part to the same page
  • Grab the ID in the __doPostBack call and replace it in this script, and put the script into the form web part and save.
<div style="width:400px; height:20px; margin:0px auto; padding-bottom:20px; font-family:Verdana,Arial,Helvetica,Sans-serif;font-size:10pt;">
 Select Doc Type: 
 <select id="ddlDocType">

<script type='text/javascript' src='/assets/js/jquery-1.7.2.min.js'></script>
<script type="text/javascript"> 

 //on the dropdown change event, call FilterMyList

function FilterMyList()
//get the selected value of the drop down
 var selectedDocType = $("#ddlDocType").val();
 if(selectedDocType == "" )
 //clears the filter
  __doPostBack("ctl00$m$g_95403266_84ab_485e_be73_8857b5d90f63$ctl02", "NotUTF8;__filter={DocType=" + "##dvt_all##"  + "}");
 //filters the list on the selected docType
 __doPostBack("ctl00$m$g_95403266_84ab_485e_be73_8857b5d90f63$ctl02", "NotUTF8;__filter={DocType=" + selectedDocType + "}");

Now, when you select a value from the dropdown, it should automatically filter the list without a full postback