Monday, July 2, 2007

How to create the Publishing pages programmatically into sharepoint

MOSS 2007 shipped with a customizable STSADM command that you can add any custom commands by using IStsadmcommand interface.
Create a console application with the following logic, Add this command to the STSADM.

Link to add the custom STSADM command:

public void FillPublishingWebWithPages (string publishingSiteCollection, int pagesToCreate)
SPSite siteCollection = null;
SPWeb site = null;
PublishingSite publishingSite = null;
PublishingWeb publishingWeb = null;
try {
// get the PublishingWeb
siteCollection = new SPSite(publishingSiteCollection);
site = siteCollection.RootWeb();
publishingSite = new PublishingSite(siteCollection);
publishingWeb = PublishingWeb.GetPublishingWeb(site);
// Article Page content type
SPContentTypeId articleContentTypeID = new SPContentTypeId("0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D");

// get the ArticleLeft.aspx Page Layout
PageLayout[] layouts = publishingWeb.GetAvailablePageLayouts(articleContentTypeID);
PageLayout articlePageLayout = layouts[1];
// create a temp name...
string pageName = DateTime.Now.ToString("yyyyMMdd-HHmmss");
// create the specified number of pages
for (int i = 0; i < pagesToCreate; i++) {
PublishingPage newPage = publishingWeb.GetPublishingPages().Add(string.Format("{0}_Gend_Page_{1}.aspx", pageName, i), articlePageLayout);
} catch (Exception ex) {

throw new Exception(ex);
// properly dispose of the resources

No comments:

Post a Comment