【发布时间】:2011-06-01 01:34:37
【问题描述】:
如何从 SharePoint 场中的网站集中获取所有 ContentType。请记住,我想使用 SharePoint 对象模型来执行此操作。任何帮助将不胜感激。
【问题讨论】:
如何从 SharePoint 场中的网站集中获取所有 ContentType。请记住,我想使用 SharePoint 对象模型来执行此操作。任何帮助将不胜感激。
【问题讨论】:
这将适用于站点中所有 SPWeb 中的所有类型。请注意,这会产生重复。
public void GetContentTypes()
{
string siteUrl = "Add site url here";
using (SPSite site = new SPSite(siteUrl))
{
foreach (SPWeb web in site.AllWebs)
{
foreach (SPContentType item in web.ContentTypes)
{
Debug.WriteLine(item.Name);
}
foreach (SPList list in web.Lists)
{
foreach (SPContentType item in list.ContentTypes)
{
Debug.WriteLine(item.Name);
}
}
web.Dispose();
}
}
}
【讨论】:
可以这样做:
public void ListContentTypes(string siteUrl)
{
try
{
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb())
{
ListContentTypes(web);
}
}
}
catch (Exception ex)
{
// add some proper error handling here
}
}
public void ListContentTypes(SPWeb web)
{
foreach (SPContentType ct in web.ContentTypes)
{
// do whatever you want to do with the content type here
}
foreach (SPWeb subWeb in web.Webs)
{
try
{
ListContentTypes(subWeb);
}
finally
{
if (subWeb != null)
{
subWeb.Dispose();
}
}
}
}
这将查找网站集中存在的所有内容类型,但请记住,并非所有内容类型都在整个网站集中可用。例如:如果您的子站点中存在内容类型“产品”,则上面的代码会找到它,但您将无法在根网站中使用它,因为它是在较低级别中定义的。
【讨论】:
试试这个:urWeb.AvailableContentTypes
【讨论】: