I have a requirement to add several pages to a WCM enabled MOSS 2007 site programatically. To do this I needed to add page items to the pages list in the WCM site. To access the pages list you need to first get hold of the relevant site and cast it to a PublishingWeb. Once that has been done the content type is found and the page layout is chosen. At that point the page can be created, have values assigned to it and even be published and approved programatically.
The code to do this is:
//get site to add page to
SPSite Site = new SPSite(siteName);
SPWeb Web = Site.OpenWeb();
//cast it to a publishing site
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(Web)
//get Page content type - open the relevant content type to get it's GUID
SPContentTypeId ContentTypeID = new SPContentTypeId("0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3
DB064584E219954237AF39005A25699F214C32448043AC003089FFB0");
//get page layout
PageLayout[] layouts = publishingWeb.GetAvailablePageLayouts(ContentTypeID);
PageLayout pageLayout = layouts[0];
//create a new page
PublishingPage newPage = publishingWeb.GetPublishingPages().Add("newPageName" + ".aspx", pageLayout);
newPage.ListItem["columnName"] = "Some Value"
SPFile listItemFile = page.ListItem.File;
//check that the file is not checked out - if it is check it in. This is more important when you have been modifying existing pages
if (listItemFile.CheckOutStatus != SPFile.SPCheckOutStatus.None)
{
listItemFile.CheckIn("");
}
listItemFile.Publish("");
listItemFile.Approve("");
Thread.Sleep(1000);
Web.Dispose();
Site.RootWeb.Dispose();
Site.Dispose();
Thread sleep is there in case you are adding many pages at once as this causes problems for the variations job and only the first page added is propagated. By adding a sleep the pages are added more slowly and the variation job works as expected!