I need to programmatically create a page. I did research online and found some code that should create page, so I’ve added this code to features’ event receiver. The code is below. I assume that once I deploy the project with this feature the page I’m trying to create should appear there under SitePages library. But the page is not there after I deploy, deactivate/activate feature… This is a screenshot of that sitePages list, but the “SamplePage” was not created there (sorry, I was not able to upload the image).

So I'm puzzled if this code is not sufficient to have the page created on sharepoint site, or may be it creates page somewhere else, not under Site Page. Or may be there are some additional steps I have to take to make the code working correctly.

Public override void FeatureActivated(SPFeatureReceiverProperties properties)

    using (SPSite site = (SPSite)properties.Feature.Parent)
    using (SPWeb oWebsite = site.OpenWeb())
    string xml = GetCreateWebPartPage("SitePages","SamplePage", 2);
    catch (Exception ex)
    pmoWeb.AllowUnsafeUpdates = false;
    privatestring GetCreateWebPartPage(String list, string pageTitle,int layoutTemplate)
    conststring 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;

Here is the code I have used to create a Web Page page. Use the template instead of XML. There should be 9 templates to choose from in the SMARTPGS directory. This was tested in 2010.

    public static void CreateWebPartPage(SPWeb web, string pageName, string libraryName)

        string templateFilename = "spstd1.aspx";
        string hive = SPUtility.GetGenericSetupPath("TEMPLATE\\1033\\STS\\DOCTEMP\\SMARTPGS\\");
        FileStream stream = new FileStream(hive + templateFilename, FileMode.Open);

        SPFolder libraryFolder = web.GetFolder(libraryName);
        SPFileCollection files = libraryFolder.Files;
        SPFile newFile = files.Add(pageName, stream);

I suggest you try your code in button click method and see if it is working properly. If it does not work there, that means definitely there is a problem with your code.

Here are some links to check too: http://blogs.msdn.com/b/sridhara/archive/2008/06/21/create-and-publish-web-pages-in-publishing-sharepoint-sites-programmatically.aspx


  • Thank you very much for a very good advice... The code below from Colbs has worked, but will be glad to use your methods next time. Thank you again! – novice Jul 2 '14 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.