Home > C#, MOSS 2007 > WebPartPages: Programmatically adding a new web part page

WebPartPages: Programmatically adding a new web part page


How do you add a WebPartPage or a BasicPage programmatically?

WebPartPages are stored in document libraries. It is simply a matter of adding the page to the document library you want. However, if you need to add a WebPartPage programmatically the same way SharePoint allows you to do via the UI then you can use the following method:


private void AddWebPartPage(string fileTitle, SPWeb web, SPList list, int webPartPageTemplate, string pageType, string folder)
{
    string postInformation =
    "<?xml version="1.0" encoding="UTF-8"?>" +
    "<Method>" +
    "<SetList Scope="Request">" + list.ID + "</SetList>" +
    "<SetVar Name="ID">New</SetVar>" +
    "<SetVar Name="Cmd">NewWebPage</SetVar>" +
    "<SetVar Name="Type">" + pageType + "</SetVar>" +
    "<SetVar Name="WebPartPageTemplate">" + webPartPageTemplate + "</SetVar>" +
    "<SetVar Name="Title">" + fileTitle + "</SetVar>" +
    "<SetVar Name="Overwrite">true</SetVar>" +
    "</Method>";
    string processBatch = web.ProcessBatchData(postInformation);
    if (processBatch.Equals("<Result ID="" Code="0">rn</Result>n"))
    {
        SPFile file = web.GetFile(list.RootFolder.Url + "/" + fileTitle + ".aspx");
        //if page was in subfolder then move it there
        if (!String.IsNullOrEmpty(folder))
        {
            file.MoveTo(fileTitle,
true);
        }
    }
}

Below are the WebPartPage templates you can specify:

  1. Full Page, Vertical
  2. Header, Footer, 3 Columns
  3. Header, Left Column, Body
  4. Header, Right Column, Body
  5. Header, Footer, 2 Columns, 4 Rows
  6. Header, Footer, 4 Columns, Top Row
  7. Left Column, Header, Footer, Top Row, 3 Columns
  8. Right Column, Header, Footer, Top Row, 3 Columns

Type could have the following possible values:

  1. WebPartPage
  2. BasicPage

For further details click here.

Advertisements
  1. iMe
    May 12, 2009 at 3:55 pm

    Thx, just what i was looking for. 🙂

  1. April 5, 2009 at 8:09 pm
  2. February 18, 2015 at 3:26 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: