Click here to Skip to main content
15,886,075 members
Articles / Programming Languages / C++

Create a Web Part Page Programmatically

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
4 May 2013CPOL 27.4K   1   1
How to create a web part page programmatically

Introduction

When creating SharePoint Solutions, we need to deploy Web part pages with the solution. I'm sure most of us deploy pages as a module. But we are copying aspx page from somewhere else and include it to module. (Visual Studio does not allow to create web part aspx pages in SharePoint Solutions).

So I think it is good to use a method to create web part pages rather than copying from somewhere else and put it to the solution. Using the below method, you can add web part page to document library with specified template. Of course, you can add custom template; in that case you can deploy custom template first and use the following method to create a web part page.

First, you need to generate the XML for add Web Part Page:

C++
private string GetCreateWebPartPage(String list, string pageTitle, int layoutTemplate)
{
   const string newItemTemplate = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                                  "<Batch>" +
                                  "<Method ID=\"0,NewWebPage\">" +
                                   "<SetList Scope=\"Request\">{0}</SetList>" +
                                   "<SetVar Name=\"Cmd\">NewWebPage</SetVar>" +
                                   "<SetVar Name=\"ID\">New</SetVar>" +
                                   "<SetVar Name=\"Type\">WebPartPage</SetVar>" +
                                   "<SetVar Name=\"WebPartPageTemplate\">{2}</SetVar>" +
                                   "<SetVar Name=\"Overwrite\">true</SetVar>" +
                                   "<SetVar Name=\"Title\">{1}</SetVar>" +
                                   "</Method>" +
                                    "</Batch>";
   var newItemXml = string.Format(newItemTemplate, list, pageTitle, layoutTemplate); 

   return newItemXml;
}

Then pass it to ProcessBatchData method in SPWeb to create the page.

C++
using (SPWeb oWebsite = SPContext.Current.Site.OpenWeb("Website_URL"))
{
   string xml = GetCreateWebPartPage("SitePages", "SamplePage", 2);
   oWebsite.ProcessBatchData();
}

You can put the code in Feature Activation for deployment.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Sri Lanka Sri Lanka
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionerror Pin
Member 769689022-Nov-13 3:50
Member 769689022-Nov-13 3:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.