您需要更改表示站点的 SPWeb 对象的 MasterUrl 属性。执行此操作的一个好方法是创建一个 SharePoint 功能,该功能在激活时设置属性,而在停用时恢复原始值。Feature.xml 可能如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Feature Id="8651CC66-FEB1-4255-B7E9-0DFE24367DAB"
Title="My Master Page"
Scope="Web"
SolutionId="06D3B01F-0C26-457a-BFA5-A1B0BC8D4225"
ReceiverAssembly="MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9e95445247029289"
ReceiverClass="MyFeatureReceiver"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="Masterpage.xml"/>
<ElementFile Location="my.master"/>
</ElementManifests>
<Properties>
<Property Key="MyMaster" Value="my.master" />
</Properties>
</Feature>
Masterpage.xml 文件将您的母版页上传到图库:
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="UploadMaster" Url="_catalogs/masterpage" >
<File Url="my.master" Type="GhostableInLibrary" IgnoreIfAlreadyExists="True"/>
</Module>
</Elements>
将此功能与包含 MyFeatureReceiver 类的 MyAssembly.dll 一起包含在 WSP 解决方案中,如下所示:
public class MyFeatureReceiver : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = (SPWeb)properties.Feature.Parent;
web.MasterUrl = properties.Definition.Properties["MyMaster"].Value;
web.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWeb web = (SPWeb)properties.Feature.Parent;
web.MasterUrl = "default.master";
web.Update();
}
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
}
}
最后,部署解决方案并在您的站点上激活该功能。