Sharepoint: how can i find all the pages that host a particular web part?

吃可爱长大的小学妹 提交于 2019-12-30 05:03:57

问题


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


回答1:


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!




回答2:


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


来源:https://stackoverflow.com/questions/633633/sharepoint-how-can-i-find-all-the-pages-that-host-a-particular-web-part

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!