I developed some code that uploads a new page layout file (ASPX) from disk to the Master Page Gallery of a site collection. I wanted to associate the uploaded publishing page with an existing content type, such as the “Welcome Page” content type. This is equivalent to editing the properties of the page layout and setting the “Associated Content Type” property…
Setting the Associated Content Type property in code is not as easy as thought. After trawling around the web for an hour, I found no good example. I found plenty of examples to create a Page Layout declaratively in XML and assign the associated content property, but no examples in C# for an existing SPFile object. Then it hit me…
The PublishingAssociatedContentType property expects a formatted string that contains both the display name and the content type ID. So, armed with both an SPFile object that is the page layout file and an SPContentType object, I was able to associate the page layout with the following code:
file.Item.Properties["PublishingAssociatedContentType"] = String.Format(";#{0};#{1};#", spCT.Name, spCT.Id.ToString());
Make sure to call file.Item.Update();
Wham!
That unnamed project has caused many head scratching moments.
Pingback: Sharepoint Updates November-04-2011 | SDavara's Sharepoint Knowledge Center