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: http://sharepointsolutions.blogspot.com/2006/09/extending-stsadmexe-with-custom.html


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);
newPage.Update();
}
} catch (Exception ex) {

throw new Exception(ex);
}
finally
{
// properly dispose of the resources
site.Dispose();
siteCollection.RootWeb.Dispose();
siteCollection.Dispose();
}
}

No comments:

Post a Comment