检索页面上的Web部件列表

时间:2009-10-05 19:52:57

标签: sharepoint wss-3.0 web-parts

我正在尝试获取在sharepoint 3.0中的网页上部署的webpart列表。有没有办法可以从sharepoint内容数据库中检索它,还是可以通过编程方式进行检索?

2 个答案:

答案 0 :(得分:8)

您可以使用SPWebPartManager通过页面中的Web部件列表进行迭代。

请参阅this MSDN example

编辑: 这可能是一个更好的例子:

private static void GetWebParts()
{
  using (SPSite site = new SPSite("<YOUR SITE URL>"))
  {
    using (SPWeb web = site.OpenWeb())
    {
      SPFile file = web.GetFile("default.aspx"); // or what ever page you are interested in
      using (SPLimitedWebPartManager wpm = file.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
      {
        foreach (WebPart wp in wpm.WebParts)
        {
          Console.WriteLine("Web part: {0}", wp.Title);
        }
      }
    }
  }
}

答案 1 :(得分:-2)

以编程方式添加Web部件很简单:

SPWeb site = SPContext.Current.Web;
SPFile page = web.GetFile("Pages/somepage.aspx");
using (SPLimitedWebPartManager webPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
{
    try
    {
        // logic to get web parts  here. 
        ContentEditorWebPart webPart = new ContentEditorWebPart();      
        webPart.Title = "Test Web Part"; 
        webPartManager.AddWebPart(webPart, "Zone 1", 0);
    }
    finally
    {
        // SPLimitedWebPartManager has known memory leak where it does not dispose SPRequest object in its SPWeb, so dispose it
        webPartManager.Web.Dispose();
    }
}