SPContentTypeId.CompareTo Method
Compares the current content type identifier (ID) with another content type ID.
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
Syntax
'Declaration
Public Function CompareTo ( _
id As SPContentTypeId _
) As Integer
'Usage
Dim instance As SPContentTypeId
Dim id As SPContentTypeId
Dim returnValue As Integer
returnValue = instance.CompareTo(id)
public int CompareTo(
SPContentTypeId id
)
Parameters
id
Type: Microsoft.SharePoint.SPContentTypeIdA content type ID to compare to the current content type ID. The method throws an ArgumentException if the argument is an object of another type.
Return Value
Type: System.Int32
An integer that indicates the relative order of the objects that are being compared. The return value can have one of the following meanings:
Value |
Meaning |
---|---|
Less than zero |
The current content type ID is less than the ID that was passed as an argument. |
Zero |
The two content IDs are equal. |
Greater than zero |
The current content type ID is greater than the ID that was passed as an argument. |
Remarks
Use this method to compare two content type IDs. Note that the ID of a base content type has a lesser value than the ID of any content type that is derived from it.
Examples
The following example shows a console application that prints a list of site content types organized in a way that is similar to the list on the Site Content Types Gallery. The output is sorted first by content type group, then by content type ID. In order to accomplish the sort, the example implements the IComparer interface. The implementation first compares the group names of two content types. If the names are the same, it then compares the content type IDs by calling the CompareTo method of the SPContentTypeId class.
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
The application prints the following output to the console.
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...
See Also
Reference
Microsoft.SharePoint Namespace