tirsdag 24. februar 2009

Create Sharepoint WebPart Pages Programatically

Create Sharepoint WebPart Pages Programatically:

Ok so if anyone has wanted to create SharePoint 2007 webpart pages programatically would have probably come up against a few challenges.

Business Case:

A Windows service would create new dashboard pages and add Excel Web Access Web Parts to the page.

Step 1 - Create a Web Part page Programatically.

Open Visual Studio (Im using 2008) and create a console application.

Add the following references and then the corrosponding using statements bellow:

using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Administration;

Add the following code (I constucted this as public methods so I called call them from a test project but up to you)

public void FillPublishingWebWithPages(string publishingSiteCollection, int pagesToCreate)
string Page = null;
string ZoneID = null; ;
string workbookURI = null;
SPSite SiteCollection = null;
SPWeb site = null;
PublishingSite publishingSite = null;
PublishingWeb publishingWeb = null;
// 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");
SPContentTypeId articleContentTypeID = new SPContentTypeId("0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF390064DEA0F50FC8C147B0B6EA0636C4A7D4");
// 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++)
//Page = publishingWeb.GetPublishingPages().Add(string.Format("{0}_Aldersfordelt_{1}.aspx", pageName, i), articlePageLayout).ToString();
PublishingPage newPage = publishingWeb.GetPublishingPages().Add(string.Format("{0}_Aldersfordelt_{1}.aspx", pageName, i),articlePageLayout);
catch (Exception ex)
throw new Exception(ex.Message);
// properly dispose of the resources
workbookURI = "http://mossutv1/Rapports/Aldersfordelt/Aldersfordelt%2023.11.2008%20(2008-11-30%2000_50_53).xlsx";
ZoneID = "1";
AddEWAWebPart ae = new AddEWAWebPart();
Page = "http://mossutv1:88/Pages/20090224-140052_Aldersfordelt_0.aspx";
ae.Prepare(Page, workbookURI, ZoneID);

Ovbiously you will have to change the Page, WorkBookURI, ZoneID and URL headings etc....

Step 2: Create the Class for the AddEWAWebPart method prepare you see above.

Ok so this is simple, just right create a class ("I called mine AddEWAWebPart") then create a public method or private which ever you are working with. And use the code found from Microsoft on this link.


Remember that your not using a console app, so you will have to collect the parameters as I have send to the prepare method above.

Any help just let me know.

James Bruiners