SPContentTypeUsage.GetUsages 方法

返回有关指定的内容类型中使用的信息与SPContentTypeUsage对象的列表。

命名空间:  Microsoft.SharePoint
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
Public Shared Function GetUsages ( _
    contentType As SPContentType _
) As IList(Of SPContentTypeUsage)
用法
Dim contentType As SPContentType
Dim returnValue As IList(Of SPContentTypeUsage)

returnValue = SPContentTypeUsage.GetUsages(contentType)
public static IList<SPContentTypeUsage> GetUsages(
    SPContentType contentType
)

参数

返回值

类型:System.Collections.Generic.IList<SPContentTypeUsage>
SPContentTypeUsage对象的集合。

备注

此方法返回SPContentTypeUsage对象,其中包含有关每个使用中的网站集的内容类型的信息的泛型列表。如果未使用的内容类型,该方法将返回一个空列表 (Count = 0)。

备注

A content type is "used" if any content type derived from it is present in an SPContentTypeCollection collection at the site or list level anywhere within its scope. For more information, see Content Type Scope.

示例

下面的示例是一个控制台应用程序获取的使用率集合的内置内容类型"项。应用程序计算的次数用作网站内容类型和它用作列表内容类型,然后打印到控制台总计次数。

Imports System
Imports System.Collections.Generic
Imports Microsoft.SharePoint

Module ConsoleApp
   Sub Main()
      Using siteCollection As SPSite = New SPSite("https://localhost")
         Using rootWeb As SPWeb = siteCollection.RootWeb

            ' Get the content type.
            Dim contentType As SPContentType = _
               rootWeb.AvailableContentTypes(SPBuiltInContentTypeId.Item)

            'Get the usage collection.
            Dim usages As IList(Of SPContentTypeUsage) = _
               SPContentTypeUsage.GetUsages(contentType)

            ' Count the site and list types.
            Dim listTypes As Integer = 0
            Dim siteTypes As Integer = 0
            For Each usage As SPContentTypeUsage In usages
               If usage.IsUrlToList Then
                  listTypes += 1
               Else
                  siteTypes += 1
               End If
            Next usage

            Console.Write("The content type is inherited by {0} site content types", siteTypes)
            Console.WriteLine(" and {0} list content types.", listTypes)

         End Using
      End Using
      Console.Write(vbCrLf + "Press ENTER to continue...")
      Console.ReadLine()
   End Sub

End Module
using System;
using System.Collections.Generic;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite siteCollection = new SPSite("https://localhost"))
         {
            using (SPWeb rootWeb = siteCollection.RootWeb)
            {
               // Get the content type.
               SPContentType contentType =
                  rootWeb.AvailableContentTypes[SPBuiltInContentTypeId.Item];

               //Get the usage collection.
               IList<SPContentTypeUsage> usages = SPContentTypeUsage.GetUsages(contentType);

               // Count the site and list types.
               int listTypes = 0;
               int siteTypes = 0;
               foreach (SPContentTypeUsage usage in usages)
               {
                  if (usage.IsUrlToList)
                     listTypes++;
                  else
                     siteTypes++;
               }

               Console.Write("The content type is inherited by {0} site content types", siteTypes);
               Console.WriteLine(" and {0} list content types.", listTypes);
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }
   }
}

应用程序运行时对使用工作组网站模板创建网站,其打印到控制台以下输出。

The content type is inherited by 33 site content types and 20 list content types.

Press ENTER to continue...

另请参阅

引用

SPContentTypeUsage 类

SPContentTypeUsage 成员

Microsoft.SharePoint 命名空间

其他资源

Site and List Content Types

Creating Content Types Based on Other Content Types