检索页面上的 Web 部件列表

发布于 2024-08-06 23:37:45 字数 85 浏览 2 评论 0原文

我正在尝试获取部署在 sharepoint 3.0 网页上的 webpart 列表。有没有办法从共享点内容数据库中检索它,或者我可以通过编程方式执行此操作?

I am trying to get a list of webparts deployed on a web page in sharepoint 3.0. Is there way I can retrieve it from sharepoint content database or can I do it programmatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧夏天 2024-08-13 23:37:45

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

请参阅此 MSDN 示例

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

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);
        }
      }
    }
  }
}

You can use the SPWebPartManager to iterate thru a list of web part in a page.

See this MSDN example.

EDIT:
This is maybe a better 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);
        }
      }
    }
  }
}
风吹短裙飘 2024-08-13 23:37:45

以编程方式添加 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();
    }
}

Adding web parts programmatically is simple:

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文