共用方式為


SPFolder.UniqueContentTypeOrder Property

Sets or gets an ordered list of content types.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

Public Property UniqueContentTypeOrder As IList(Of SPContentType)
    Get
    Set

Dim instance As SPFolder
Dim value As IList(Of SPContentType)

value = instance.UniqueContentTypeOrder

instance.UniqueContentTypeOrder = value
public IList<SPContentType> UniqueContentTypeOrder { get; set; }

Property Value

Type: System.Collections.Generic.IList<SPContentType>
A System.Collections.Generic.List<SPContentType> object that represents a list of content types.

Exceptions

Exception Condition
InvalidOperationException

The folder’s ParentListId property returns an empty GUID. This happens when the folder is not part of a list. Setting or getting the value of the UniqueContentTypeOrder property has meaning only in the context of a list.

ArgumentOutOfRangeException

You are attempting to set the property with an empty list. The list must contain at least one content type.

Remarks

The value returned by the UniqueContentTypeOrder property is identical to the value returned by the ContentTypeOrder property. Setting the UniqueContentTypeOrder property also sets the ContentTypeOrder property. Both properties determine the sequence in which content types are listed in the user interface, such as on a Sharepoint list’s New menu or on the List Settings page.

You can reset the folder’s content type order to the default order by setting the UniqueContentTypeOrder property to a null value, as in the following code snippet.

list.RootFolder.UniqueContentTypeOrder = null;

Tip

You can hide a content type by setting the UniqueContentTypeOrder property with a list that omits the content type that you wish to hide.

Examples

The following example is a console application that first prints the current content type order to the console, then reverses the content type order, and finally prints the new content type order to the console. The application assumes the existence of a site with a list named “Test Library” that contains one or more content types.

Imports System
Imports Microsoft.SharePoint

Module ConsoleApp

    Sub Main()
        Console.WriteLine()

        Dim site As SPSite = New SPSite("http://siteUrl")
        Dim web As SPWeb = site.OpenWeb()
        Dim list As SPList = web.Lists("Test Library")

        ' Get the current order and print it to the console
        Dim currentOrder As System.Collections.Generic.IList(_
                Of SPContentType) = _
                New System.Collections.Generic.List(Of SPContentType)
        currentOrder = list.RootFolder.ContentTypeOrder
        Console.WriteLine("Old order:")
        For Each ct As SPContentType In currentOrder
            Console.WriteLine(ct.Name)
        Next

        ' Create a new order by reversing current order
        Dim newOrder As System.Collections.Generic.IList(Of SPContentType) =_
                        New System.Collections.Generic.List(Of SPContentType)
        For i As Integer = currentOrder.Count - 1 To 0 Step -1
            newOrder.Add(currentOrder(i))
        Next

        'Set the new order and update folder
        list.RootFolder.UniqueContentTypeOrder = newOrder
        list.RootFolder.Update()

        Console.WriteLine("New order:")
        For Each ct As SPContentType In list.RootFolder.ContentTypeOrder
            Console.WriteLine(ct.Name)
        Next

        ' Clean up
        web.Dispose()
        site.Dispose()

        Console.WriteLine()
        Console.Write("Press ENTER to continue...")
        Console.ReadLine()
    End Sub

End Module
using System;
using Microsoft.SharePoint;

namespace Test
{
    class ConsoleApp
    {
        static void Main(string[] args)
        {
            Console.WriteLine();

            SPSite site = new SPSite("http://siteUrl");
            SPWeb web = site.OpenWeb();
            SPList list = web.Lists["Test Library"];

            // Get the current order and print it to the console
            System.Collections.Generic.IList<SPContentType> currentOrder =
                      new System.Collections.Generic.List<SPContentType>();
            currentOrder = list.RootFolder.ContentTypeOrder;
            Console.WriteLine("Old order:");
            foreach (SPContentType ct in currentOrder)
            {
                Console.WriteLine(ct.Name);
            }
            Console.WriteLine();

            // Create a new order by reversing current order
            System.Collections.Generic.IList<SPContentType> newOrder =
                      new System.Collections.Generic.List<SPContentType>();
            for (int i = currentOrder.Count - 1; i > -1; i--)
            { 
                newOrder.Add(currentOrder[i]);
            }

            // Set the new order and update folder
            list.RootFolder.UniqueContentTypeOrder = newOrder;
            list.RootFolder.Update();

            // Display the new current order
            Console.WriteLine("New order:");
            foreach (SPContentType ct in list.RootFolder.ContentTypeOrder)
            {
                Console.WriteLine(ct.Name);
            }

            // Clean up.
            web.Dispose();
            site.Dispose();

            Console.WriteLine();
            Console.Write("Press ENTER to continue...");
            Console.ReadLine();
        }
    }
}

When run against a list with two content types, Presentation and Proposal, the application prints the following output to the console.

Old order:
Presentation
Proposal

New order:
Proposal
Presentation

Press ENTER to continue...

See Also

Reference

SPFolder Class

SPFolder Members

Microsoft.SharePoint Namespace