Archive for March, 2013

March 31, 2013

Publishing pages based on a SharePoint Article page layout programmatically

Glad I got some few minutes to update my blog after 2 months hence was in a busy schedule. J  Please refer the below code for you to create a Publishing page on Sharepoint 2010 based on a page layout which is available in your site.

PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
string pageName = “MyPageName.aspx”;
PageLayout[] pageLayouts = publishingWeb.GetAvailablePageLayouts();
PageLayout selectedLayout = null;
foreach (PageLayout pl in pageLayouts)
if (pl.Name == "MyPublishingPageLayout.aspx")
selectedLayout = pl;
PageLayout currPageLayout = selectedLayout;
PublishingPageCollection pages = publishingWeb.GetPublishingPages();
PublishingPage newPage = pages.Add(pageName,currPageLayout);
newPage.ListItem[FieldId.PublishingPageContent] = “Content of the page”;
newPage.CheckIn(“Comment for the checkin”);
newPage.ListItem.File.Publish("page published");
newPage.ListItem.File.Approve("page approved");

please refer the following links for more details: