Question

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.

Était-ce utile?

La solution

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);
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top