Sharepoint:如何找到承载特定 Web 部件的所有页面?

Posted

技术标签:

【中文标题】Sharepoint:如何找到承载特定 Web 部件的所有页面?【英文标题】:Sharepoint: how can i find all the pages that host a particular web part? 【发布时间】:2010-10-12 15:05:36 【问题描述】:

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

【问题讨论】:

【参考方案1】:

如果您正在寻找代码,我可以为您准备好东西。如果您想查找所有内容查询 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();
        
    

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

【讨论】:

顺便说一句,您不应该通过单击复选标记将我的答案标记为答案吗? 请注意,此代码仅检查“页面”文档库中的页面。如果不进行一些修改,将不会检查存储在其他文档库中的页面或站点的 default.aspx 页面 (yoursite.com/site/default.aspx)。感谢您的帖子,对我很有帮助,并节省了我一些时间来研究 API。【参考方案2】:

作为替代方法,如果您想测试 Web 部件页面,包括协作站点上的默认页面,您可以使用以下代码 sn-p,它使用 SPWeb 对象的 Files 属性:

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

注意:发布功能创建的页面库没有 SPListTemplateType.DocumentLibrary 的 BaseTemplate 值;相反,它由 850 的“隐藏”值表示。

这与 LeonZandman 的回答类似,但仅使用类名来提供匹配项:

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

【讨论】:

以上是关于Sharepoint:如何找到承载特定 Web 部件的所有页面?的主要内容,如果未能解决你的问题,请参考以下文章