[英]how to add a WebPart to all pages in a SharePoint site?
我正在将SharePiont Server 2007企业版与Windows Server 2008企业版一起使用,并且正在使用发布门户网站模板。 我正在使用VSTS 2008 + C#+ .Net 3.5开发。 我想知道如何将WebPart添加到SharePoint网站的所有页面吗? 有参考样品吗?
我想使用此WebPart在所有页面上显示一些常用信息(但该信息可能会动态更改,这就是为什么我选择WebPart的原因)。
有两种方法可以执行此操作,具体取决于您的情况。
如果站点已经存在,则需要遍历站点,并添加Web部件:
http://blogs.msdn.com/tconte/archive/2007/01/18/programmatically-adding-web-parts-to-a-page.aspx
如果站点不存在,则可以将Web部件添加到站点模板中:
这是设拉子第一个链接的代码,结果得到了更多:
(注意:此代码未经过优化,例如,通常不应该遍历List的Items集合,但是看到这可能只是一次操作,就没有问题了)
private void AddCustomWebPartToAllPages()
{
using(SPSite site = new SPSite("http://sharepoint"))
{
GetWebsRecursively(site.OpenWeb());
}
}
private void GetWebsRecursively(SPWeb web)
{
//loop through all pages in the SPWeb's Pages library
foreach(var item in web.Lists["Pages"].Items)
{
SPFile f = item.File;
SPLimitedWebPartManager wpm = f.GetLimitedWebPartManager(PersonalizationScope.Shared);
//ADD YOUR WEBPART
YourCustomWebPart wp = new YourCustomWebPart();
wp.YourCustomWebPartProperty = propertyValue;
wpm.AddWebPart(wp, "ZONEID", 1);
f.Publish("Added Web Part");
f.Approve("Web Part addition approved");
}
// now do this recursively
foreach(var subWeb in web.Webs)
{
GetWebsRecursively(subWeb);
}
web.Dispose();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.