Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

The following is updated code from Ton Stegeman and it works.

 private static void AddContentType(SPWeb web, SPList spList, string ctName)
    {
        if (web.AvailableContentTypes[(ctName)] != null)
        {                
            SPContentType ContentType = web.AvailableContentTypes[ctName];
            spList.ContentTypesEnabled = true;
            spList.Update();
            spList.ContentTypes.Add(ContentType);
            spList.Update();

            SPFolder rootFolder = spList.RootFolder;
            SPContentType ct = spList.ContentTypes[ctName];
            List<SPContentType> defaultCT = new List<SPContentType>();
            defaultCT.Add(ct);
            rootFolder.UniqueContentTypeOrder=defaultCT;
            rootFolder.Update();


        }
    }
share|improve this question
    
Beware other readers: this removes all content types from the New Menu except the one specified in the method's argument. –  James Love May 31 '11 at 18:03

3 Answers 3

Here's some utility code I use to reorder content types:

private void ReorderContentTypes(SPWeb web, string listName, string firstContentTypeName)
    {
        SPList list = web.Lists[listName];

        SPContentType cType = web.AvailableContentTypes[firstContentTypeName];

        List<SPContentType> oldCTypes = new List<SPContentType>();

        for (int i = list.ContentTypes.Count -1; i >= 0; i--)
        {
            if (!list.ContentTypes[i].Id.IsChildOf(cType.Id))
            {
                oldCTypes.Add(list.ContentTypes[i]);

                list.ContentTypes[i].Delete();
            }
        }

        foreach (SPContentType c in oldCTypes)
        {
            list.ContentTypes.Add(c);
        }

        list.Update();
    }
share|improve this answer
    
No, List<SPContentType> oldCTypes = new List<SPContentType>(); creates a new List (read: a collection) of SPContentTypes called oldCTypes. SPList is not involved on this particular line of code. –  James Love May 31 '11 at 16:16
    
I need to keep the existing CTs. So your code wont work as you are deleting CTs. I just need to make customCT default and number 1 in order. –  Robert G May 31 '11 at 16:48
    
My code re-adds them. Look at the foreach. –  James Love May 31 '11 at 17:34
    
Perfect, just what i needed. –  Anders Aune Oct 6 '11 at 8:17

You can specify the order of the content types in the SPFolder.UniqueContentTypeOrder property of the root folder (SPList.RootFolder).

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfolder.uniquecontenttypeorder.aspx

share|improve this answer
    
You can only "get" that property, it's read only. –  James Love May 31 '11 at 14:53
    
yes, but maybe with UniqueContentTypeOrder property –  Matthias May 31 '11 at 15:12
    
You'd still have to build a List of ContentTypes and assign it to that property. I had issues with that, which is why I went with the method of deleting all the content types except the desired one for the default, adding them to a new list, and then re-adding them to the list's content types. –  James Love May 31 '11 at 15:22
    
What's the matter of building a list of ContentTypes? What issues did you have? –  Matthias May 31 '11 at 15:26
    
Building the list of content types was easy, it was assigning it to UniqueContentTypeOrder, can't remember the issues I had but I reverted to the method I posted above. –  James Love May 31 '11 at 15:32

This might be of use:

http://www.novolocus.com/2009/05/26/control-the-content-types-in-the-new-menu-via-code/

I was trying to control what was shown in the menu, but I was also defining the order (in the solution I was building), and what was in the list, and then assigning it to the UniqueContentTypeOrder.

I found that when trying to set the UniqueContentTypeOrder, you need to use SPList.ContentTypes to get the content type to set in the UniqueContentTypeOrder list, and not use one from the SPWeb.ContentTypes collection. If you use the SPWeb one I found you got an ArgumentOutOfRangeException.

share|improve this answer
    
I think that was the issue I had, if you look at my code I'm getting SPWeb.AvailableContentTypes. I then also check for IsChildOf, because List ContentTypes are children of the respective Site ContentTypes. –  James Love May 31 '11 at 15:45
    
FWIW: I voted this up, as it's a proper solution, and probably the way you're meant to do it, along with useful advice about where to get your contenttypes in the first place. Who voted this down and why? –  James Love May 31 '11 at 17:38
    
Maybe because it doesn't address just the ordering issue, but also what was on the list - it makes it a little less obvious? Or parhaps 'cos I didn't include all the code in here? There didn't seem much point repeating it all. Thanks for the vote though James! FWIW, it worked for me. –  Andy Burns Jun 1 '11 at 8:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.