Sharepoint:如何找到托管特定 Web 部件的所有页面?

发布于 2024-07-15 01:28:29 字数 39 浏览 6 评论 0原文

正如问题所述 - 有没有办法确定哪些页面包含我的 Web 部件?

As the question says - is there a way to determine which pages are including my web part?

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

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

发布评论

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

评论(2

淡看悲欢离合 2024-07-22 01:28:29

如果您正在寻找代码,我可以为您提供一些东西。 如果您想查找所有内容查询 Web 部件,那么您可以像这样调用我的代码:

FindWebPart("http://server.com/", "Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart");

这是代码:

public static void FindWebPart(string siteCollectionUrl, string webPartName)
{
    using (SPSite siteCollection = new SPSite(siteCollectionUrl))
    {
        using (SPWeb rootSite = siteCollection.OpenWeb())
        {
            FindWebPartHelper(rootSite, webPartName);
        }
    }
}

public static void FindWebPartHelper(SPWeb site, string webPartName)
{
    // Search for web part in Pages document library
    SPList pagesList = null;
    try
    {
        pagesList = site.Lists["Pages"];
    }
    catch (ArgumentException)
    {
        // List not found
    }

    if (null != pagesList)
    {
        SPListItemCollection pages = pagesList.Items;
        foreach (SPListItem page in pages)
        {
            SPFile file = page.File;
            using (SPLimitedWebPartManager mgr = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
            {
                try
                {
                    SPLimitedWebPartCollection webparts = mgr.WebParts;
                    foreach (System.Web.UI.WebControls.WebParts.WebPart wp in webparts)
                    {
                        // Here perform the webpart check
                        // For instance you could identify the web part by
                        // its class name

                        if (webPartName == wp.GetType().ToString())
                        {
                            // Found a match! Now do something...
                            Console.WriteLine("Found web part!");
                        }
                    }
                }
                finally
                {
                    // Needs to be disposed
                    mgr.Web.Dispose();
                }

            }
        }
    }

    // Check sub sites
    SPWebCollection subSites = site.Webs;
    foreach (SPWeb subSite in subSites)
    {
        try
        {
            FindWebPartHelper(subSite, webPartName);
        }
        finally
        {
            // Don't forget to dispose!
            subSite.Dispose();
        }
    }
}

当然,您可以对此代码进行少量更改。 目前它进行字符串比较,但以更类型化的方式进行比较很容易。 玩得开心!

If you're looking for code, I've got something for you. If you'd like to find all Content Query web parts then you would call my code like this:

FindWebPart("http://server.com/", "Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart");

Here's the code:

public static void FindWebPart(string siteCollectionUrl, string webPartName)
{
    using (SPSite siteCollection = new SPSite(siteCollectionUrl))
    {
        using (SPWeb rootSite = siteCollection.OpenWeb())
        {
            FindWebPartHelper(rootSite, webPartName);
        }
    }
}

public static void FindWebPartHelper(SPWeb site, string webPartName)
{
    // Search for web part in Pages document library
    SPList pagesList = null;
    try
    {
        pagesList = site.Lists["Pages"];
    }
    catch (ArgumentException)
    {
        // List not found
    }

    if (null != pagesList)
    {
        SPListItemCollection pages = pagesList.Items;
        foreach (SPListItem page in pages)
        {
            SPFile file = page.File;
            using (SPLimitedWebPartManager mgr = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
            {
                try
                {
                    SPLimitedWebPartCollection webparts = mgr.WebParts;
                    foreach (System.Web.UI.WebControls.WebParts.WebPart wp in webparts)
                    {
                        // Here perform the webpart check
                        // For instance you could identify the web part by
                        // its class name

                        if (webPartName == wp.GetType().ToString())
                        {
                            // Found a match! Now do something...
                            Console.WriteLine("Found web part!");
                        }
                    }
                }
                finally
                {
                    // Needs to be disposed
                    mgr.Web.Dispose();
                }

            }
        }
    }

    // Check sub sites
    SPWebCollection subSites = site.Webs;
    foreach (SPWeb subSite in subSites)
    {
        try
        {
            FindWebPartHelper(subSite, webPartName);
        }
        finally
        {
            // Don't forget to dispose!
            subSite.Dispose();
        }
    }
}

Ofcourse you can make little changes to this code. Currently it does a string comparison, but it's easy to do it in a more typed way. Have fun!

小嗷兮 2024-07-22 01:28:29

作为替代方案,如果您想要测试 Web 部件页面(包括协作站点上的默认页面),您可以使用以下代码片段,该代码片段使用 SPWeb 对象的 Files 属性:

private static void FindWebPart(string siteUrl, string webPartName)
{
    using (var site = new SPSite(siteUrl))
    {
        foreach (SPWeb web in site.AllWebs)
        {

            foreach (var file in web.Files.Cast<SPFile>().Where(file => file.Name.EndsWith("aspx")))
            {
                FindWebPartOnPage(webPartName, file);
            }

            var pagesTemplateType = (SPListTemplateType)Enum.Parse(typeof(SPListTemplateType), "850");
            foreach (var documentLibrary in web.Lists.Cast<SPList>().Where(list => list.BaseTemplate == pagesTemplateType || (list.BaseTemplate == SPListTemplateType.DocumentLibrary && list.Title.Contains("Pages"))))
            {
                foreach (var file in documentLibrary.Items.Cast<SPListItem>().Where(item => item.File.Name.EndsWith("aspx")).Select(item => item.File))
                {
                    FindWebPartOnPage(webPartName, file);
                }
            }

            web.Dispose();
        }
    }
}

private static void FindWebPartOnPage(string webPartName, SPFile file)
{
    using (var webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
    {
        if (webPartManager.WebParts.Cast<WebPart>().Any(webPart => webPart.GetType().Name == webPartName))
        {
            Console.WriteLine(file.ServerRelativeUrl);
        }

        webPartManager.Web.Dispose();
    }
}

注意:由发布功能创建的页面库不会没有 SPListTemplateType.DocumentLibrary 的 BaseTemplate 值; 相反,它由“隐藏”值 850 表示。

这与 LeonZandman 的答案类似,但仅使用类名来提供匹配:

FindWebPart("http://yoursite.com/", "MyWebPart");

By way of an alternative, if you want to test web part pages including the default page on collaboration sites, you can use the following code snippet which uses the Files property of the SPWeb object:

private static void FindWebPart(string siteUrl, string webPartName)
{
    using (var site = new SPSite(siteUrl))
    {
        foreach (SPWeb web in site.AllWebs)
        {

            foreach (var file in web.Files.Cast<SPFile>().Where(file => file.Name.EndsWith("aspx")))
            {
                FindWebPartOnPage(webPartName, file);
            }

            var pagesTemplateType = (SPListTemplateType)Enum.Parse(typeof(SPListTemplateType), "850");
            foreach (var documentLibrary in web.Lists.Cast<SPList>().Where(list => list.BaseTemplate == pagesTemplateType || (list.BaseTemplate == SPListTemplateType.DocumentLibrary && list.Title.Contains("Pages"))))
            {
                foreach (var file in documentLibrary.Items.Cast<SPListItem>().Where(item => item.File.Name.EndsWith("aspx")).Select(item => item.File))
                {
                    FindWebPartOnPage(webPartName, file);
                }
            }

            web.Dispose();
        }
    }
}

private static void FindWebPartOnPage(string webPartName, SPFile file)
{
    using (var webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
    {
        if (webPartManager.WebParts.Cast<WebPart>().Any(webPart => webPart.GetType().Name == webPartName))
        {
            Console.WriteLine(file.ServerRelativeUrl);
        }

        webPartManager.Web.Dispose();
    }
}

Note: the Pages library created by the Publishing feature does not have a BaseTemplate value of SPListTemplateType.DocumentLibrary; instead it is represented by a "hidden" value of 850.

This is called a similar manner to LeonZandman's answer, however only the class name is used to supply a match:

FindWebPart("http://yoursite.com/", "MyWebPart");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文