Create and Publish web pages in Publishing SharePoint sites programmatically
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("https://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");
}
}
This is an easy way of automating creating & publishing pages in collaboration/publishing sites, especially when the need is to create/publish them at a larger scale. I hope this tip is helpful!