以编程方式在Sharepoint中实例化Web部件页面

时间:2009-06-11 06:29:19

标签: c# sharepoint wss-3.0

是否有一种简单的方法可以使用对象模型或Web服务以编程方式将Web部件页面添加到Sharepoint站点?以这种方式创建列表和添加Web部件似乎很简单,但我找不到如何创建内容页面的示例。

编辑:对于普通的WSS安装(不是MOSS)。

3 个答案:

答案 0 :(得分:13)

我将采取这个不是协作/发布网站的路线,因为没有提及,而且wss在标签列表中。与使用发布网站相比,相当笨拙...

首先选择您要使用的网页部分页面模板:

  

C:\ Program Files \ Common   Files \ Microsoft Shared \ web服务器   扩展\ 12 \ TEMPLATE \ 1033 \ STS \ DOCTEMP \ SMARTPGS

然后设置流到模板并使用SPFileCollection.Add()将其添加到文档库。例如:

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);
}

注意:此解决方案假定您安装了使用1033语言代码的美国SharePoint版本。如果不同,只需改变路径。

答案 1 :(得分:2)

答案 2 :(得分:0)

@AlexAngas接受的答案的替代解决方案是使用NewWebPage methodSharePoint Foundation RPC Protocol,如建议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);
}

使用上述扩展方法:

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