Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 are including my web part?

like image 549
nailitdown Avatar asked Mar 11 '09 07:03

nailitdown


2 Answers

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!

like image 92
LeonZandman Avatar answered Oct 06 '22 01:10

LeonZandman


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");
like image 35
OiDatsMyLeg Avatar answered Oct 06 '22 02:10

OiDatsMyLeg