Showing posts with label SharePoint 2010. Show all posts
Showing posts with label SharePoint 2010. Show all posts

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 https://gist.github.com/sparsee/0bb2218a2b42ba679d12b96ce7d645ae
  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

Note:
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: https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/branding-and-site-provisioning-solutions-for-sharepoint
    • 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') {
//browseris.ie = false;
browseris.ie5up = false;
}
}
</script>

Tuesday, January 22, 2013

Determine which SharePoint Web Front End Server you are hitting

Often, we need to identify which WFE server on SharePoint we are hitting when the farm has load balancing.

I decided to create a very simple feature that would display the server name by overriding the GlobalNavigation delegate control of each page that was being accessed across every site collection and sub sites in a web application.

When accessing any page, you will see the server name similar to below:




First, I created the user control, and added one simple label:

<asp:Label ID="lblServerName" runat="server" BackColor="Green" ForeColor="White"></asp:Label>

In the code behind, I put code to check whether the current user is a farm administrator, or if the query parameter, "showserver" has been passed into the url. If either are true, then I display the name of the sharepoint server on the label:

 protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (IsFarmAdmin() || ServerParamIsSet())
                {
                    this.lblServerName.Width = new Unit("100%");
                    this.lblServerName.Text = string.Format("{0}", Page.Server.MachineName);
                }
            }
            catch { }
        }



Next, I added an Elements manifest, and specified the path to where the control template will be deployed to on the server, a sequence number lower than 100, and specified it to place the user control in the GlobalNavigation delegate control placeholder.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Control Id="GlobalNavigation" Sequence="90" ControlSrc="~/_ControlTemplates/SPServerName/SPServerName.ascx" />
</Elements>


Finally, I created a feature and set it to deploy to the web application.


Installation instructions:
1. Download the wsp I uploaded to http://spservername.codeplex.com/
2. Deploy the solution to your farm
3. Go to the web application features, and enable the SPServerName feature


Notes:
  • There are no changes made to any master page and the feature can easily be turned on and off
  • By default, Farm administrators will always see the server name at the top of the page.
  • Non-farm administrators can also display the server name if they pass in the query parameter "showserver=1"
Ex:

http://myportal.com/siteabc?showserver=1