以编程方式在Sharepoint中实例化Web部件页面
有没有一种简单的方法可以使用对象模型或Web服务以编程方式将Web部件页面添加到Sharepoint站点? 以这种方式创建列表和添加Web部件似乎很简单,但我找不到如何创建内容页面的示例。
编辑:对于普通的WSS安装(不是MOSS)。
我将采取这个不是协作/发布网站的路线,因为这没有提到,而且wss在标签列表中。 与使用发布网站相比,相当笨重……
首先选择您要使用的Web部件页面模板:
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版本。 如果不同,只需改变路径。
它是一个合作/发布网站吗? 如果是这样,您可以在以下博客文章中提供帮助:
- 以编程方式在发布SharePoint网站中创建和发布网页
- 以编程方式创建页面 – 和添加Web部件
- 以编程方式将页面添加到MOSS发布站点
@AlexAngas接受的答案的另一种解决方案是使用SharePoint Foundation RPC协议的NewWebPage方法 ,如此处所示 。
private static void CreateWebPartPage(this SPWeb web, SPList list, string pageName, int layoutTemplate) { const string newWPPage = "" + "" + "" + "{0} " + "NewWebPage " + "New " + "WebPartPage " + "{2} " + "true " + "{1} " + " " + " "; var newWPPageBatchXml = string.Format(newWPPage, list.ID, pageName, layoutTemplate); var result = web.ProcessBatchData(newWPPageBatchXml); }
使用上述扩展方法:
web.CreateWebPartPage(yourList, "NewPage", 2);