Adding Pages to SharePoint Site Programatically


To add pages to a Publishing site you first need to get an instance of a PublishingWeb object. Then, you need to get a content type (this wasn’t so obvious because the SPContentTypeID object isn’t documented in the WSS v3 SDK… as of the latest available SDK at least [Beta 2 Tech Refresh]), a page layout, and finally create the page.

Here’s the sample code… have at it! It only fills the root site in the site collection with pages, but it should be pretty clear to any SharePoint developer how to add pages to subsites as well. It assumes the site collection was created using the Publishing Portal site template (from MOSS 2007 B2TR) because it expects both the Article Page content type and ArticleLeft.aspx page layout exist.

   1:  public void FillPublishingWebWithPages (string publishingSiteCollection, int pagesToCreate) {
   2:    SPSite siteCollection = null;
   3:    SPWeb site = null;
   4:    PublishingSite publishingSite = null;
   5:    PublishingWeb publishingWeb = null;
   6:   
   7:    try {
   8:      // get the PublishingWeb
   9:      siteCollection = new SPSite(publishingSiteCollection);
  10:      site = siteCollection.RootWeb();
  11:      publishingSite = new PublishingSite(siteCollection);
  12:      publishingWeb = PublishingWeb.GetPublishingWeb(site);
  13:   
  14:      // Article Page content type
  15:      SPContentTypeId articleContentTypeID = new SPContentTypeId("0x010100C568DB52D9D0A1
4D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D");
  16:      
  17:      // get the ArticleLeft.aspx Page Layout 
  18:      PageLayout[] layouts = publishingWeb.GetAvailablePageLayouts(articleContentTypeID);
  19:      PageLayout articlePageLayout = layouts[1];
  20:      
  21:      // create a temp name...
  22:      string pageName = DateTime.Now.ToString("yyyyMMdd-HHmmss");
  23:      
  24:      // create the specified number of pages
  25:      for (int i = 0; i < pagesToCreate; i++) {
  26:        PublishingPage newPage = publishingWeb.GetPublishingPages().Add(
string.Format("{0}_Gend_Page_{1}.aspx", pageName, i), articlePageLayout);
  27:        newPage.Update();
  28:      }
  29:    } catch (Exception ex) {
  30:      throw new Exception(ex);
  31:    } finally {
  32:      // properly dispose of the resources
  33:      site.Dispose();
  34:      siteCollection.RootWeb.Dispose();
  35:      siteCollection.Dispose();
  36:    }
  37:  }

Web Content Management Web Casts 3 Part Series


Part I WCM for Content Owners and Authors [OCT-3]

Part II WCM for Site Admins & Owners [OCT-10]

Microsoft Office SharePoint Server 2007 Web Content Management for Developers and Designers

The Web Content Management (WCM) technology in the upcoming 2007 version of Microsoft Office SharePoint Server (MOSS) represents the first major release of Microsoft Content Management Server (MCMS) since 2002. This webcast looks at developer and designer oriented tasks in WCM such as creating field controls, creating and customizing master pages and page layouts. In addition, we’ll look at how you can customize the HTML Editor and Page Edit Toolbar and enforce rules on your authors such as allowing only certain styles within specific field controls. We’ll also cover what tools you’ll use and when to use them in developing your WCM sites, such as Visual Studio 2005 and SharePoint Designer 2007.

Part III MSDN Webcast: MOSS 2007 WCM for Site Developers & Designers

WSS Version 3.0 and MOSS 2007 Download


Note: Installing Microsoft Windows SharePoint Services 3.0 over any beta or trial edition of Microsoft Office SharePoint Server 2007 is not supported.

Links to download the RTW versions:

Microsoft Office SharePoint Server 2007 x86 English Evaluation 

Windows SharePoint Services 3.0


Here are the Product Keys you will need when installing SharePoint Server 2007

SharePoint Server Standard Trial: XJMKW-8T7PR-76XT6-RTC8G-VVWCQ
SharePoint Server Enterprise Trial: F2JBW-4PDJC-HKXTJ-YCKRP-T2J9D 

Installation Instructions

For WSS 3.0 refer to the Windows SharePoint Services in Windows Server 2003 site for installation and deployment details.

Installation guidance on Microsoft Office SharePoint 2007 refer to the following 

Build to Build Upgrade Resources

Version to Version Upgrade & Migration

Related Resources: