Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a task where I need to locate every page that has a particular web part on it.

It looks like I may be able to get this info from the database itself but I'm not sure how thorough this may be. Is there a better method for doing this?

Thanks Mike

share|improve this question
add comment

2 Answers

Answer can be found from Stackexchange, here's copy & link to the answer. You can create a simple console application that you run on the server.

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

Of course 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."

Source

share|improve this answer
add comment

You can use SPLimitedWebPartManager to get information about the WebPart location and metadata.

For example you can iterate all the pages and use this method to find your WebPart:

public static void AddWebPartToPage(SPWeb web, string pageUrl, string webPartName)
    {
        using (SPLimitedWebPartManager webPartManager = web.GetLimitedWebPartManager(pageUrl, PersonalizationScope.Shared))
        {
            SPLimitedWebPartCollection coll = webPartManager.WebParts;
            foreach (Microsoft.SharePoint.WebPartPages.WebPart webpart in coll)
            {
                if (webPartName == webpart.GetType().Name)
                {
                    //your logic here
                }

            }
        }
    }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.