以编程方式设置HtmlDesignAssociated值
-
28-09-2020 - |
题
我正在从服务器部署一个组成的外观(包括。spcolor,。spfont,主人。html和母版。预览文件),但我无法获取要选择的字段关联文件。我试过把它放在 elements.xml
文件以及事件接收器的 FeatureActivated
事件。
元素。xml:
<File Path="Master\corporate.html" Url="corporate.html" Type="GhostableInLibrary" ReplaceContent="True">
<Property Name="ContentType" Value="Html Master Page"></Property>
<Property Name="UIVersion" Value="15"></Property>
<Property Name="HtmlDesignAssociated" Value="TRUE"></Property>
<Property Name="FSObjType" Value="0"></Property>
</File>
和代码隐藏:
SPList masterGallery = web.Lists.TryGetList("Master Page Gallery");
SPListItemCollection masterItems = masterGallery.Items;
for (int m = 0; m < masterItems.Count; m++)
{
SPListItem page = masterItems[m];
if(page["Name"].ToString() == "corporate.html")
{
page["HtmlDesignAssociated"] = true;
page.Update(); // forgot to include this line
}
}
注:我遗漏了其他不相关的代码。
解决方案
好吧,显然是想设置字段 HtmlDesignAssociated
从 elements.xml
模块中的文件将不起作用。
一旦我把它拿出来,事件接收器中的代码就工作了,我的 master.html
文件现在有一个关联的 master.master
.
我稍微修改了一下代码以利用 SPUrlUtility.CombineUrl
而不是 web.GetList
或 web.Lists.TryGetList("whatever")
.这是有效的代码:
var masterGallery = SPUrlUtility.CombineUrl(relativePath, "_catalogs/masterpage");
SPList master = web.GetList(masterGallery);
SPListItemCollection masterItems = master.Items;
for (int m = 0; m < masterItems.Count; m++)
{
SPListItem page = masterItems[m];
if (page["Name"].ToString() == "corporate.html")
{
page["HtmlDesignAssociated"] = true;
page.Update();
}
}