Monday 7 May 2012

Apply custom masterpage in sharepoint 2010 programmatically

It's been very easy to apply master page in sharepoint 2010 using C#

Follow this steps.

Add new sharepoint empty project.
After that Add new feature with feature receiver.


add this code in FeatureActivated method like so

public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {

            //Get Parent Web
            SPWeb Web = properties.Feature.Parent as SPWeb;
            Web.AllowUnsafeUpdates = true;
            string strRelativeURL = (Web.ServerRelativeUrl.EndsWith("/") ? Web.ServerRelativeUrl : Web.ServerRelativeUrl + "/");

            //Apply default and custom masterpage URL
            Web.MasterUrl = strRelativeURL + "_catalogs/masterpage/masterpage.master";
            Web.CustomMasterUrl = strRelativeURL + "_catalogs/masterpage/masterpage.master";

            //Apply the alternate css
            Web.AlternateCssUrl = css path;

            //Confirm the version
            Web.UIVersion = 4;
            Web.Update();
            Web.AllowUnsafeUpdates = false;
      }

No comments:

Post a Comment