当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《Sharepoint:我如何找到托管特定Web部件的所有页面?》经验,为你挑选了1个好方法。

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



1> LeonZandman..:

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

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

这是代码:

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

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

推荐阅读
个性2402852463
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有