Sharepoint:如何找到承载特定 Web 部件的所有页面? [英] Sharepoint: how can i find all the pages that host a particular web part?
本文介绍了Sharepoint:如何找到承载特定 Web 部件的所有页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
正如问题所说 - 有没有办法确定哪些页面包含我的 Web 部件?
As the question says - is there a way to determine which pages are including my web part?
推荐答案
如果您正在寻找代码,我可以为您准备一些东西.如果您想查找所有内容查询 Web 部件,那么您可以这样调用我的代码:
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");
代码如下:
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!
这篇关于Sharepoint:如何找到承载特定 Web 部件的所有页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文