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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/979635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:45:03  来源:igfitidea点击:

Programmatically instantiate a web part page in Sharepoint

c#sharepointwss-3.0

提问by thade

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.

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

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

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

采纳答案by Alex Angas

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...

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

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

首先从以下位置选择要使用的 Web 部件页面模板:

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

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:

然后为模板设置一个流并使用 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);
}

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

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

回答by pholpar

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

另一种解决方案,从@AlexAngas接受的答案是使用NewWebPage方法中的SharePoint基础RPC协议,如建议在这里

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