Control the Content Types in the New menu via code

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();
        }
    }
}

 

Another One:
1    private void SortContentTypes(SPList docLibrary)
2    {
3        IList<SPContentType> currentOrder = docLibrary.RootFolder.ContentTypeOrder;
4        List<SPContentType> newOrder = currentOrder.ToList<SPContentType>();
5        newOrder.Sort((x, y) => (x.Name.CompareTo(y.Name)));
6        docLibrary.RootFolder.UniqueContentTypeOrder = newOrder;
7        docLibrary.RootFolder.Update();
8    }

 

References:

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfolder.uniquecontenttypeorder%28v=office.12%29.ASPX

https://www.nothingbutsharepoint.com/sites/devwiki/articles/pages/sharepoint-add-content-types-to-a-document-library-and-then-sort-them-programmatically.aspx

Advertisements
This entry was posted in SharePoint 2010, Visual Studio 2010. Bookmark the permalink.

One Response to Control the Content Types in the New menu via code

  1. Click here. says:

    Heya i’m for the first time here. I found this board and I find It really useful &
    it helped me out a lot. I hope to give something back and help oghers like you helped me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s