Provisionate MasterPage and CSS with Feature

Posted on Updated on

Sometimes, when you don’t want to use Powershell, you can set your branding from a Feature, it’s the old procedure but very “elastic”.

In the bellow sample, I’ll give you the option to change you master page to Front Office and Back Office, you can also define your CSS File.

I’m processing all Sub Webs also, just change what you want. This is a Feature SCOPE Site

public override void FeatureActivated(SPFeatureReceiverProperties properties)
    using (var site = properties.Feature.Parent as SPSite)
        if (site == null) return;
        using (var web = site.OpenWeb())
            var masterUrl = web.Site.ServerRelativeUrl == "/" ? 
                "/_catalogs/masterpage/YOURCustomFO.master" : web.Site.ServerRelativeUrl + 
            var customMasterUrl = web.Site.ServerRelativeUrl == "/" ? 
                "/_catalogs/masterpage/seattle.master" : web.Site.ServerRelativeUrl + 
            //string cssUrl = web.Site.ServerRelativeUrl == "/" ? "/Style Library/xx.css" : web.Site.ServerRelativeUrl + "/Style Library/xx.css";

            #region MasterPage e CSS

            // Define Master page for FO and BO
            web.CustomMasterUrl = masterUrl;
            web.MasterUrl = customMasterUrl;

            //Alternate CSS URL 
            //web.AlternateCssUrl = cssUrl;

            // inheritance settings
            web.AllProperties["__InheritsMasterUrl"] = "True";
            web.AllProperties["__InheritsCustomMasterUrl"] = "True";
            //web.AllProperties["__InheritsAlternateCssUrl"] = "True";


            foreach (SPWeb subWeb in web.Webs)
                    if (subWeb != null)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s