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

6
有没有一种简单的方法可以使用对象模型或Web服务以编程方式将Web部件页面添加到Sharepoint站点?以这种方式创建列表和添加Web部件似乎很简单,但我找不到如何创建内容页面的示例。
编辑:适用于普通WSS安装(而非MOSS)。
3个回答

12

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

首先从以下位置选择您想要使用的Web Part页面模板:

C:\ Program Files \ Common Files \ Microsoft Shared \ web server extensions \ 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。 如有不同,请更改路径。


@Alex,能否将newpage.aspx的主页面设置为SomeMasterPageName.master?我该如何在代码中包含它? - Carls Jr.


0

对于@AlexAngas所接受的答案,另一种解决方案是使用SharePoint Foundation RPC ProtocolNewWebPage方法,如此处所建议。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接