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

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

+5
source share
2 answers

If you are looking for code, I have something for you. If you want to find all Content Query Web Parts, you can call my code as follows:

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

Here is 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();
        }
    }
}

Of course, you can make small changes to this code. It currently does string comparisons, but it's easy to do this in a more typical way. Enjoy!

+15
source

, -, , , Files SPWeb:

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

. Pages, , BaseTemplate SPListTemplateType.DocumentLibrary; "" 850.

LeonZandman, :

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

All Articles