2009-06-11 80 views
7

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

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

回答

13

我打算走的路线,这是不是一个协作/发布网站因为这没有提及,wss在标签列表中。相较于使用发布网站非常笨重......

首先选择你想从使用Web部件页面模板:

C:\ Program Files文件\共同 文件\微软共享\ 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版本。如果不同,就改变路径。

+0

它的工作原理:d !!好帖子亚历克斯:-)! – Muhammedh 2009-12-10 11:33:21

0

@AlexAngas接受的答案的替代解决方案是使用SharePoint Foundation RPC ProtocolNewWebPage method,建议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); 
相关问题