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

nai*_*own 5 sharepoint web-parts

正如问题所说 - 有没有办法确定哪些页面包含我的网页部分?

Leo*_*man 15

如果您正在寻找代码,我会为您提供一些帮助.如果您想查找所有Content Query Web部件,那么您可以调用我的代码:

FindWebPart("http://server.com/", "Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart");
Run Code Online (Sandbox Code Playgroud)

这是代码:

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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