Programmatically instantiate a web part page in Sharepoint

六眼飞鱼酱① 提交于 2019-11-29 03:28:03

问题


Is there a simple way to add a web part page to a Sharepoint site programmatically, using either the object model or web services? It seems straight-forward to create lists and add web parts in this manner, but I can't find an example of how to create a content page.

Edit: For a plain WSS installation (not MOSS).


回答1:


I'm going to take the route that this isn't a collaboration/publishing site as this isn't mentioned and wss is in the tag list. Pretty clunky in comparison to using a publishing site...

First choose the web part page template you'd like to use from:

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\1033\STS\DOCTEMP\SMARTPGS

Then set up a stream to the template and use SPFileCollection.Add() to add it to your document library. For example:

string newFilename = "newpage.aspx";
string templateFilename = "spstd1.aspx";
string hive = SPUtility.GetGenericSetupPath("TEMPLATE\\1033\\STS\\DOCTEMP\\SMARTPGS\\");
FileStream stream = new FileStream(hive + templateFilename, FileMode.Open);
using (SPSite site = new SPSite("http://sharepoint"))
using (SPWeb web = site.OpenWeb())
{
    SPFolder libraryFolder = web.GetFolder("Document Library");
    SPFileCollection files = libraryFolder.Files;
    SPFile newFile = files.Add(newFilename, stream);
}

Note: This solution assumes you have the US SharePoint version installed that uses the 1033 language code. Just change the path if different.




回答2:


Is it a collaboration/publishing site? If so you can the following blog articles should help:

  • Create and Publish web pages in Publishing SharePoint sites programmatically
  • Programmatically create pages - and Add Web Parts
  • Programmatically adding pages to a MOSS Publishing site



回答3:


An alternative solution to the accepted answer from @AlexAngas is to use the NewWebPage method of the SharePoint Foundation RPC Protocol, as suggested here.

private static void CreateWebPartPage(this SPWeb web, SPList list, string pageName, int layoutTemplate)
{
    const string newWPPage = "<?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 newWPPageBatchXml = string.Format(newWPPage, list.ID, pageName, layoutTemplate);

    var result = web.ProcessBatchData(newWPPageBatchXml);
}

Usage of the above extension method:

web.CreateWebPartPage(yourList, "NewPage", 2);


来源:https://stackoverflow.com/questions/979635/programmatically-instantiate-a-web-part-page-in-sharepoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!