I am trying to create new pages in my publishing site(need to use a custom page layout) using SharePoint Server Object Model. I couldn't find much help on this topic from internet.
Happy to have some references from you.
I am trying to create new pages in my publishing site(need to use a custom page layout) using SharePoint Server Object Model. I couldn't find much help on this topic from internet.
Happy to have some references from you.
Microsoft.SharePoint.Publishing is the assembly that we need to use to get pages created and published. A sample is provided below:
using (SPSite site = new SPSite("http://moss"))
{
using (SPWeb web = site.OpenWeb())
{
PublishingSite pSite = new PublishingSite(site);
SPContentType ctype = pSite.ContentTypes["Welcome Page"];
PageLayoutCollection pageLayouts = pSite.GetPageLayouts(ctype, true);
PageLayout pageLayout = pageLayouts["/_catalogs/masterpage/welcomesplash.aspx"];
PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web);
PublishingPageCollection pPages = pWeb.GetPublishingPages();
PublishingPage pPage = pPages.Add("Programmatic_Test.aspx", pageLayout);
SPListItem newpage = pPage.ListItem;
newpage["Title"] = "Page added programmatically";
newpage.Update();
newpage.File.CheckIn("all looks good");
newpage.File.Publish("all looks good");
}
}