SharePoint Internals – Hristo Pavlov’s Blog

7 August, 2008

Changing the default list content type programatically

This is something I wanted to do today and couldn’t find any obvious way to accomplish it. Checking the code behind of the ChangeContentTypeOrder.aspx page revealed how it can be done. You need to build a SPContentType[] array or List<SPContentType> list that contains the content types in the order you like. The first content type in the list will be used as a default content type for the SPList. Once you have the array/list you need to assign it to the SPFolder.UniqueContentTypeOrder member of the RootFolder of the list and then call SPFolder.Update() to apply your new order.

You cannot use any content types that inherit from one of these two content types:

SPBuiltInContentTypeId.Folder – 0x0120

SPBuiltInContentTypeId.UntypedDocument0x010104

The dummy example below reverses the order of the content types of a list and also uses another cool thing I learned from checking the code behind of the ChangeContentTypeOrder.aspx page – the cool .NET sorting method Array.Sort<TKey, TValue> that sorts two arrays simultaneously based on the first array. Of course I could have also used Array.Reverse() method to do the same.

SPContentType[] newContentTypeOrderArray = new SPContentType[list.RootFolder.ContentTypeOrder.Count];

int[] newContentTypeOrderIndexes = new int[list.RootFolder.ContentTypeOrder.Count];

 

for (int i = 0; i < list.RootFolder.ContentTypeOrder.Count; i++)

{

    newContentTypeOrderArray[i] = list.RootFolder.ContentTypeOrder[i];

    newContentTypeOrderIndexes[i] = -i;

}

 

Array.Sort<int, SPContentType>(newContentTypeOrderIndexes, newContentTypeOrderArray);

 

list.RootFolder.UniqueContentTypeOrder = newContentTypeOrderArray;

list.RootFolder.Update();

As mentioned above you should be also aware that the “Folder” and “Untyped Document” content types are not shown on the “Change New Button Order and Default Content Type” page however they may become a default content type of the list if you delete all other content types or make one of them default content type programmatically. In this situation if the default content type is “Folder” for example and you upload a file to the document library the file’s content type will be “Folder” – probably not something you really wanted to happen🙂

Finally if you delete the “Folder” content type from a list you may experience some other strange things. See my EditForm Not Shown When Uploading a File post for details.

4 Comments »

  1. […] Changing the default list content type programmatically […]

    Pingback by Links (8/20/2008) « Steve Pietrek - Everything SharePoint — 21 August, 2008 @ 12:50 am

  2. […] Content type Hello AlexZ, Default content type is the first from all CT if your library https://hristopavlov.wordpress.com/20…ogramatically/ — WBR, Michael Nemtsev [Microsoft MVP] :: blog: http://msmvps.org/blogs/laflour :: […]

    Pingback by Content type | keyongtech — 18 January, 2009 @ 4:23 pm

  3. In case anyone was curious, if you follow TDD practices and use TypeMock for SharePoint development here is how to put this piece of code under unit test.

    SPList fakeList = Isolate.Fake.Instance();

    SPContentType fakeDocumentContentType = Isolate.Fake.Instance();
    SPContentType fakeDIBProductContentType = Isolate.Fake.Instance();

    Isolate.WhenCalled(() => fakeDocumentContentType.Name).WillReturn(“Document”);
    Isolate.WhenCalled(() => fakeDIBProductContentType.Name).WillReturn(“Product”);

    Isolate.WhenCalled(() => fakeList.RootFolder.ContentTypeOrder).WillReturnCollectionValuesOf(new[] { fakeDocumentContentType, fakeDIBProductContentType });
    Isolate.WhenCalled(()=> fakeList.RootFolder.ContentTypeOrder.Count).WillReturn(2);

    SPContentType[] newContentTypeOrderArray = new SPContentType[fakeList.RootFolder.ContentTypeOrder.Count];
    int[] newContentTypeOrderIndexes = new int[fakeList.RootFolder.ContentTypeOrder.Count];
    for (int i = 0; i < fakeList.RootFolder.ContentTypeOrder.Count; i++)
    {
    newContentTypeOrderArray[i] = fakeList.RootFolder.ContentTypeOrder[i];
    newContentTypeOrderIndexes[i] = -i;
    }
    Array.Sort(newContentTypeOrderIndexes, newContentTypeOrderArray);

    fakeList.RootFolder.UniqueContentTypeOrder.Clear();
    fakeList.RootFolder.UniqueContentTypeOrder = newContentTypeOrderArray;
    fakeList.RootFolder.Update();

    Assert.IsTrue(newContentTypeOrderArray[0].Name == “Product”);
    Assert.IsTrue(newContentTypeOrderArray[1].Name == “Document”);

    Comment by Eric Malamisura — 6 January, 2010 @ 4:21 pm

  4. This is all great, and I expected there would be a C# way to make “Unknown Content Type” the default for a given List, but I don’t see what the context of your code is.

    How do you see the “code-behind” the ChangeContentTypeOrder.aspx?

    Assuming that you’re not changing WSS code directly here, how do I create a Feature in VS2008 that deploys your code?

    Sorry it’s not a very interesting question, but I’m just not seeing the back door you’re making use of here (and I’m in a phase of seeing how far I can get with software-free software in WSS 3 at the moment).

    Comment by Graham — 22 June, 2010 @ 1:54 pm


RSS feed for comments on this post. TrackBack URI

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

Blog at WordPress.com.

%d bloggers like this: