Settting a Master Page for a WSS SharePoint site

Published 22 July 08 11:58 PM | abartucciotto 

Just a had a colleague ask me how to set the Master Page for a WSS site. I had to cast my mind back to a project I worked on a while ago in which two techniques were used to set the Master Page. One way was editing the "Modify All Site Settings" page, accessed through the Site Actions button, which involved a modified user interface to allow an administrator to actually pick a master page from the gallery as you would in a standard Moss publishing site. This technique is actually packaged as a web part at CodePlex.

The other technique involved using features to deploy a master page to the master page gallery and then using another feature to set it as the sites master page by using code within the FeatureActivated SPFeatureReceiverProperties.

The below code from the FeatureActivated method also sets a logo and theme that I had created. This can be ommitted if you don't need to set this. As always I create an additional folder within the Images folder in the 12 hive just incase any service pack updates that are applied to a SharePoint installation overwrites my files.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb newWeb = (SPWeb)properties.Feature.Parent;

            newWeb.AllowUnsafeUpdates = true;

            //Apply web theme
            newWeb.ApplyTheme("none");
            newWeb.ApplyTheme("Your Theme");

            //Apply Logo
            newWeb.SiteLogoUrl = "/_layouts/images/YourFolder/YourLogo.gif";
            newWeb.SiteLogoDescription = "Logo Description";

            //Check if MasterPage exists in Master Page Gallery   
            foreach (SPListItem listItem in newWeb.Site.RootWeb.Lists["Master Page Gallery"].Items)
            {
                if (listItem["Name"].ToString().Equals("YourMasterPage.Master", StringComparison.OrdinalIgnoreCase))
                {
                    //Found Master Page So apply it to the current web
                    string masterUrl = new Uri(newWeb.Site.Url).GetComponents(UriComponents.Path, UriFormat.Unescaped);
                    if (!masterUrl.StartsWith("/")) masterUrl = "/" + masterUrl;
                    if (!masterUrl.EndsWith("/")) masterUrl += "/";
                    masterUrl += "_catalogs/masterpage/YourMasterPage.master";
                    newWeb.MasterUrl = masterUrl;
                    newWeb.CustomMasterUrl = masterUrl;
                }
            }

Comments

No Comments
Anonymous comments are disabled