I having multiple pages which are created using OOTB page layouts. I want to replace those with custom page layouts. Is there any way to replace the dependency of page layouts with corresponding pages?
1 Answer
You can change the page layout of any page using PowerShell or CSOM approach.
Please find below CSOM approach (it's a pseudo code):
string _newPageLayoutUrl= "new page url";
string newPageLayoutDescription ="new desc";
clientContext.Load(web);
clientContext.ExecuteQuery();
List pagesList = web.Lists.GetByTitle("Pages");
var allItemsQuery = CamlQuery.CreateAllItemsQuery();
ListItemCollection items = pagesList.GetItems(allItemsQuery);
clientContext.Load(items);
clientContext.ExecuteQuery();
foreach (ListItem item in items)
{
var pageLayout = item["PublishingPageLayout"] as FieldUrlValue;
// This will return the Full URL of Page Layout - https://.....aspx
try
{
item.File.CheckOut();
item["PublishingPageLayout"] = new FieldUrlValue() { Url = _newPageLayoutUrl, Description = newPageLayoutDescription };
item.Update();
item.File.CheckIn("comment", CheckinType.MajorCheckIn);
item.File.Publish("comments");
clientContext.ExecuteQuery();
}
catch (Exception ex)
{
}
}