-1

I would like to change master page, how would I do that in SharePoint 2013

Also, I want to do same using programming in a feature receiver.

5
  • Please add more details! What do you mean with change master page? do you mean to change the design of master page or switch between master pages , also check how to switch masterpage in SP 2013 learningsharepoint.com/2012/07/21/… Jun 3, 2017 at 13:22
  • I got the answers. I want to switch from default to custom.
    – Rinku
    Jun 3, 2017 at 14:39
  • Why negative vote?
    – Rinku
    Jun 4, 2017 at 9:24
  • Hi @Rinku your question has been flagged from the community members as low quality so it's downvoted, Please edit your question and add some details to help future visitors to understand your question Thanks! Jun 4, 2017 at 9:34
  • @M.Qassas i will take care about it.
    – Rinku
    Jun 4, 2017 at 15:16

1 Answer 1

0

If you are working in a full trust farm solution then you can programmatically apply master page.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    try
    {
        using (SPWeb currentWeb = properties.Feature.Parent as SPWeb)
        {
            using (SPSite currentSite = currentWeb.Site)
            {
                string masterPageUrl = string.Format("{0}/_catalogs/masterpage/CustomTeamSite.master", currentSite.ServerRelativeUrl).Replace("//", "/");
                currentWeb.CustomMasterUrl = masterPageUrl;
                currentWeb.MasterUrl = masterPageUrl;
                currentWeb.Update();
            }
        }
    }
    catch (Exception ex)
    {
        string errorMessage = string.Format("An exception occured while trying to activate the feature. Message: '{0}'.", ex.Message);
        throw new SPException(errorMessage);
    }
}

on deactivation of feature

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    try
    {
        using (SPWeb currentWeb = properties.Feature.Parent as SPWeb)
        {
            using (SPSite currentSite = currentWeb.Site)
            {
                string masterPageUrl = string.Format("{0}/_catalogs/masterpage/default.master", currentSite.ServerRelativeUrl).Replace("//", "/");
                currentWeb.CustomMasterUrl = masterPageUrl;
                currentWeb.MasterUrl = masterPageUrl;
                currentWeb.Update();
            }
        }
    }
    catch (Exception ex)
    {
        string errorMessage = string.Format("An exception occured while trying to deactivate the feature. Message: '{0}'.", ex.Message);
        throw new SPException(errorMessage);
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.