How to change master page in SharePoint 2013
-
23-12-2020 - |
문제
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.
해결책
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);
}
}
제휴하지 않습니다 sharepoint.stackexchange