Check out a Page Layout to update programatically
-
16-10-2019 - |
Question
In a FeatureActivated
event, I am trying to update the associated content type for a Page Layout. I am running in to issues though with the associated ASPX page not being checked out. Here's my code:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = (SPSite)properties.Feature.Parent;
if (PublishingSite.IsPublishingSite(site))
{
PublishingSite pubSite = new PublishingSite(site);
SPContentType ct = site.RootWeb.ContentTypes["Custom Page"];
string layoutURL = site.ServerRelativeUrl + "_catalogs/masterpage/" + pageLayoutName;
PageLayout layout = pubSite.PageLayouts[layoutURL];
layout.AssociatedContentType = ct;
layout.Update();
}
}
I've tried
PageLayout layout = pubSite.PageLayouts[layoutURL];
PublishingPage ppLayout = PublishingPage.GetPublishingPage(layout.ListItem);
ppLayout.CheckOut();
layout.AssociatedContentType = ct;
layout.Update();
ppLayout.CheckIn("Associated CT updated");
but this errors out with Error occurred in deployment step 'Activate Features': Invalid SPListItem. The SPListItem provided is not compatible with a Publishing Page.
Any ideas?
Solution
You need to do ListItem.CheckOut (for the list item represented by the page layout), rather than trying to check out the page object.
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange