簡體   English   中英

如何將WebPart添加到SharePoint網站中的所有頁面?

[英]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部件添加到站點模板中:

如何將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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM