Programmatically setting HtmlDesignAssociated value
-
28-09-2020 - |
Vra
I'm deploying a composed look from the server (including the .spcolor, .spfont, master.html and master.preview files), but I cannot get the field Associated File to select. I've tried setting it in just the elements.xml
file as well as the event receiver's FeatureActivated
event.
Elements.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>
And the code-behind:
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
}
}
Note: I've left out other irrelevant code.
Oplossing
Ok so apparently trying to set the field HtmlDesignAssociated
from the elements.xml
file in a Module won't work.
Once I took that out, the code in the event receiver worked and my master.html
file now has an associated master.master
.
I modified the code a little to utilize SPUrlUtility.CombineUrl
instead of web.GetList
or web.Lists.TryGetList("whatever")
. Here's the code that works:
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();
}
}