Click here to Skip to main content
11,806,026 members (54,490 online)
Click here to Skip to main content

Tagged as

Create a Web Part Page Programmatically

, 4 May 2013 CPOL 17.3K 1
Rate this:
Please Sign up or sign in to vote.
How to create a web part page programmatically


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:

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>" +
   var newItemXml = string.Format(newItemTemplate, list, pageTitle, layoutTemplate); 

   return newItemXml;

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

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

You can put the code in Feature Activation for deployment.


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


About the Author

Sri Lanka Sri Lanka
No Biography provided

You may also be interested in...

Comments and Discussions

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

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151002.1 | Last Updated 4 May 2013
Article Copyright 2013 by Melick
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid