In this post we will change the page layout of default.aspx page MOSS2010 using object modelling.
Below is the code snippet::
/// <summary>
/// Method to Change PageLayout For DefalutPage
/// </summary>
/// <param name="publishingWeb"></param>
/// <param name="currentWeb"></param>
public void ChangePageLayoutForDefalutPage(PublishingWeb publishingWeb, string customPageLayout)
{
PublishingPage publishingPage = publishingWeb.GetPublishingPage(publishingWeb.Url + "/Pages/default.aspx");
if (publishingPage != null)
{
List<PageLayout> layouts = new List<PageLayout>(publishingWeb.GetAvailablePageLayouts());
PageLayout newPageLayout = layouts.Find(
delegate(PageLayout l)
{
return l.Name.Equals(customPageLayout, StringComparison.CurrentCultureIgnoreCase);
});
if (newPageLayout != null)
{
if (publishingPage.ListItem.File.CheckOutType == SPFile.SPCheckOutType.None)
{
publishingPage.CheckOut();
}
publishingPage.Layout = newPageLayout;
publishingPage.Update();
publishingPage.CheckIn("Checked In");
publishingPage.ListItem.File.Publish("page published");
publishingPage.ListItem.File.Approve("page approved");
publishingWeb.Update();
}
}
}
Call the method as below
ChangePageLayoutForDefalutPage(mypublishingwebObj,"myPagelayout.aspx");
Hope this helps..
Below is the code snippet::
/// <summary>
/// Method to Change PageLayout For DefalutPage
/// </summary>
/// <param name="publishingWeb"></param>
/// <param name="currentWeb"></param>
public void ChangePageLayoutForDefalutPage(PublishingWeb publishingWeb, string customPageLayout)
{
PublishingPage publishingPage = publishingWeb.GetPublishingPage(publishingWeb.Url + "/Pages/default.aspx");
if (publishingPage != null)
{
List<PageLayout> layouts = new List<PageLayout>(publishingWeb.GetAvailablePageLayouts());
PageLayout newPageLayout = layouts.Find(
delegate(PageLayout l)
{
return l.Name.Equals(customPageLayout, StringComparison.CurrentCultureIgnoreCase);
});
if (newPageLayout != null)
{
if (publishingPage.ListItem.File.CheckOutType == SPFile.SPCheckOutType.None)
{
publishingPage.CheckOut();
}
publishingPage.Layout = newPageLayout;
publishingPage.Update();
publishingPage.CheckIn("Checked In");
publishingPage.ListItem.File.Publish("page published");
publishingPage.ListItem.File.Approve("page approved");
publishingWeb.Update();
}
}
}
Call the method as below
ChangePageLayoutForDefalutPage(mypublishingwebObj,"myPagelayout.aspx");
Hope this helps..