SPContentTypeId.CompareTo 方法

将当前的内容类型标识符 (ID) 与另一个内容类型 ID 进行比较

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

语法

声明
Public Function CompareTo ( _
    id As SPContentTypeId _
) As Integer
用法
Dim instance As SPContentTypeId
Dim id As SPContentTypeId
Dim returnValue As Integer

returnValue = instance.CompareTo(id)
public int CompareTo(
    SPContentTypeId id
)

参数

  • id
    类型:Microsoft.SharePoint.SPContentTypeId

    要与当前的内容类型 ID 进行比较的内容类型 ID如果参数是一个另一种类型的对象,该方法将引发ArgumentException 。

返回值

类型:System.Int32
Integer 值,该值指示进行比较的对象的相对顺序。返回的值可以具有以下含义之一:

含义

小于 0

当前的内容类型 ID 小于作为参数传递的 ID。

两个内容 Id 相等。

大于 0

当前的内容类型 ID 大于作为参数传递的 ID。

备注

使用此方法比较两个内容类型 Id。请注意基内容类型的 ID 较小的值比其派生的任何内容类型的 ID。

示例

下面的示例演示的控制台应用程序打印类似于在网站内容类型库列表方式组织的网站内容类型的列表。输出排序首先按内容类型组,然后按内容类型 id。为了实现排序,该示例可实现IComparer接口。实现首先将比较两个内容类型的组名称。如果名称相同,然后通过调用SPContentTypeId类的CompareTo方法比较内容类型 Id。

using System;
using System.Collections;
using Microsoft.SharePoint;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("https://localhost"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    // Create a sortable list of content types excluding hidden types.
                    ArrayList list = new ArrayList();
                    foreach (SPContentType ct in web.AvailableContentTypes)
                    {
                        if (ct.Group != "_Hidden")
                            list.Add(ct);
                    }
                    // Sort the list on group name.
                    list.Sort(new CTComparer());

                    // Print a report.
                    Console.WriteLine("{0,-30} {1,-12} {2}", "Site Content Type", "Parent", "Content Type ID");

                    for (int i = 0; i < list.Count; i++)
                    {
                        SPContentType ct = (SPContentType)list[i];

                        if (i == 0 || ((SPContentType)list[i - 1]).Group != ct.Group)
                        {
                            Console.WriteLine("\n{0}", ct.Group);
                            Console.WriteLine("------------------------");

                        }

                        Console.WriteLine("{0,-30} {1,-12} {2}", ct.Name, ct.Parent.Name, ct.Id);
                    }
                }
            }

            Console.Write("\nPress ENTER to continue...");
            Console.ReadLine();
        }
    }

    // Implements the Compare method from the IComparer interface.
    // Compares two content type objects by group name, then by content type Id.
    class CTComparer : IComparer
    {
        int IComparer.Compare(object x, object y)
        {
            SPContentType ct1 = (SPContentType)x;
            SPContentType ct2 = (SPContentType)y;

            // First compare group names.
            int result = string.Compare(ct1.Group, ct2.Group);
            if (result != 0)
                return result;
            // If the names are the same, compare IDs.
            return ct1.Id.CompareTo(ct2.Id);
        }
    }
}
Imports System
Imports System.Collections
Imports Microsoft.SharePoint

Namespace Test
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            Using site As New SPSite("https://localhost")
                Using web As SPWeb = site.OpenWeb()
                    ' Create a sortable list of content types excluding hidden types.
                    Dim list As New ArrayList()
                    For Each ct As SPContentType In web.AvailableContentTypes
                        If ct.Group <> "_Hidden" Then
                            list.Add(ct)
                        End If
                    Next ct
                    ' Sort the list on group name.
                    list.Sort(New CTComparer())

                    ' Print a report.
                    Console.WriteLine("{0,-30} {1,-12} {2}", "Site Content Type", "Parent", "Content Type ID")

                    For i As Integer = 0 To list.Count - 1
                        Dim ct As SPContentType = CType(list(i), SPContentType)

                        If i = 0 OrElse (CType(list(i - 1), SPContentType)).Group <> ct.Group Then
                            Console.WriteLine(vbLf & "{0}", ct.Group)
                            Console.WriteLine("------------------------")

                        End If

                        Console.WriteLine("{0,-30} {1,-12} {2}", ct.Name, ct.Parent.Name, ct.Id)
                    Next i
                End Using
            End Using

            Console.Write(vbLf & "Press ENTER to continue...")
            Console.ReadLine()
        End Sub
    End Class

    ' Implements the Compare method from the IComparer interface.
    ' Compares two content type objects by group name, then by content type Id.
    Friend Class CTComparer
        Implements IComparer
        Private Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
            Dim ct1 As SPContentType = CType(x, SPContentType)
            Dim ct2 As SPContentType = CType(y, SPContentType)

            ' First compare group names.
            Dim result As Integer = String.Compare(ct1.Group, ct2.Group)
            If result <> 0 Then
                Return result
            End If
            ' If the names are the same, compare IDs.
            Return ct1.Id.CompareTo(ct2.Id)
        End Function
    End Class
End Namespace

应用程序将以下输出显示到控制台上。

Site Content Type              Parent       Content Type ID

Document Content Types
------------------------
Document                       Item         0x0101
List View Style                Document     0x010100734778F2B7DF462491FC91844AE431CF
Form                           Document     0x010101
Picture                        Document     0x010102
Master Page                    Document     0x010105
Wiki Page                      Document     0x010108
Basic Page                     Document     0x010109
Web Part Page                  Basic Page   0x01010901
Link to a Document             Document     0x01010A
Dublin Core Columns            Document     0x01010B

Folder Content Types
------------------------
Folder                         Item         0x0120
Discussion                     Folder       0x012002
Summary Task                   Folder       0x012004

Group Work Content Types
------------------------
Circulation                    Item         0x01000F389E14C9CE4CE486270B9D4713A5D6
New Word                       Item         0x010018F21907ED4E401CB4F14422ABC65304
Resource                       Item         0x01004C9F4486FBF54864A7B0A33D02AD19B1
Official Notice                Item         0x01007CE30DD1206047728BAFD1C39A850120
Phone Call Memo                Item         0x0100807FBAC5EB8A4653B8D24775195B5463
Holiday                        Item         0x01009BE2AB5291BF4C1A986910BD278E4F18
What's New Notification        Item         0x0100A2CA87FF01B442AD93F37CD7DD0943EB
Timecard                       Item         0x0100C30DDA8EDB2E434EA22D793D9EE42058
Resource Group                 Item         0x0100CA13F2F8D61541B180952DFB25E3E8E4
Users                          Item         0x0100FBEEE6F0C500489B99CDA6BB16C398F7

List Content Types
------------------------
Item                           System       0x01
Event                          Item         0x0102
Reservations                   Event        0x0102004F51EFDEA49C49668EF9C6744C8CF87D
Schedule and Reservations      Event        0x01020072BB2A38F0DB49C3A96CF4FA85529956
Schedule                       Event        0x0102007DBDC1392EAF4EBBBF99E41D8922B264
Issue                          Item         0x0103
Announcement                   Item         0x0104
Link                           Item         0x0105
Contact                        Item         0x0106
Message                        Item         0x0107
Task                           Item         0x0108
Post                           Item         0x0110
Comment                        Item         0x0111
East Asia Contact              Item         0x0116

Special Content Types
------------------------
Unknown Document Type          Document     0x010104

Press ENTER to continue...

另请参阅

引用

SPContentTypeId 结构

SPContentTypeId 成员

Microsoft.SharePoint 命名空间

其他资源

Content Type IDs

Introduction to Content Types

Site and List Content Types

Base Content Type Hierarchy