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

I have 40 document library lists and I have to change the default document content type for these document libraries.

In UI, this is possible by

  1. On the document library, go to Settings > Document Library Settings
  2. Under content type heading, select Change new button order and default content type
  3. Move the desired document content type on first order, making it the default content type.

Programatically, so far I got document library instance as SPList, casted to SPDocumentLibrary, then pull the content type through the ContentTypes property. However, I couldn't see any property or method in the document library or content type that will do what I need.

A sample code or a reference will be greatly appreciated ;-).

share|improve this question

migrated from stackoverflow.com Jul 14 '12 at 1:03

This question came from our site for professional and enthusiast programmers.

3 Answers 3

up vote 4 down vote accepted

The first item in the content types shown on the menu is always the default.

You can manipulate the list of content types you can create items for. This actually not very obvious - it's a property of the RootFolder of the SPList. Adding or removing SPContentTypes from this property adds or remove them from the 'New' menu - but the first is always default.

SPList myList = ...
SPFolder rootFolder = myList.RootFolder;
IList<SPContentType> contentTypeList = rootFolder.ContentTypeOrder;
//Sort, add or remove content types as you want.
//e.g. contentTypeList.Add(myList.ContentTypes[contentType.Name]);
rootFolder.UniqueContentTypeOrder =  contentTypeList ;

rootFolder.Update();
myList.Update();

I've blogged about this in the past.

share|improve this answer
    
Hi, I'm currently unable to veify. I'll do so on Monday. –  OnesimusUnbound Jul 14 '12 at 0:55

For my case, I've simply deleted the first content type, therefore setting the second content type as the new content type.

I wonder if I can cast the ContentTypes property to another Collection and simply manipulate the content type within the casted form w/o deleting the anything

Here's my solution in Powershell, hopefully this will be useful to someone.

function Change-DefaultContentType() {
    process {
        # assumes that the first content type 
        # is the default content type
        $ct = $_.ContentTypes[0]
        $_.ContentTypes.Delete($ct.Id)
    }
}

$web = Get-SPWeb "url of web where the document libraries are"

# well, I can simple convert this one into a `foreach` + `if`, though I prefer 
# the piped form since I can add and remove condition or processing easily
$web.Lists `
    | ? { $_.BaseType -eq "DocumentLibrary" } `
    | ? { $_.Title -ne "MasterPages" } `
    | ? { $_.ContentTypes[0].Name -eq "current default type" } `
    | Change-DefaultContentType
share|improve this answer

I had to do exactly the same thing at our company. We had many libraries that all needed the content type default to be set to our custom Content Type. I wrote a console application that needed to be run with an account with full privelidges. My code is below for you, I hope it helps.

How my code works, it that all libraries already have the content type I wish to make default added to them. Did that with a different bit of code. This code loops through the site looking for the given content type and makes that number 1 in the ContentTypeOrder. My code also removes the microsoft Document or Page content type as in our environment we didn't need that.

 public static void SetDefaultContentTypeOnSite(string siteURL, SPContentType contentType, string listName, bool singleSite)
        {
            using (SPWeb web = new SPSite(siteURL).OpenWeb())
            {

                //Find Document Library.
                SPList splist = web.Lists.TryGetList(listName);

                if (splist != null)
                {
                    if (splist.ContentTypesEnabled)
                    {
                        Console.WriteLine(String.Format("Changing Default Content Type on {0} list on site {1} to {2}", listName, web.Title, contentType.Name));

                        SPFolder rootFolder = splist.RootFolder;
                        //GetContentType Order.
                        IList<SPContentType> ctList = rootFolder.ContentTypeOrder;


                        IList<SPContentType> result = new List<SPContentType>();

                        //If count is just 1 then it's just Document, so leave it.
                        if (ctList.Count > 1)
                        {
                            foreach (SPContentType ct in ctList)
                            {
                                if (ct.Name.Equals("Document"))
                                {
                                    splist.ContentTypes["Document"].Delete();
                                    splist.Update();
                                    continue;
                                }

                                if (ct.Name.Equals("Item"))
                                {
                                    splist.ContentTypes["Item"].Delete();
                                    splist.Update();
                                    continue;
                                }

                                if (ct.Name.Equals("Page"))
                                {
                                    splist.ContentTypes["Page"].Delete();
                                    splist.Update();
                                    continue;
                                }

                                if (ct.Name.Equals(contentType.Name))
                                    result.Insert(0, ct);
                                else if (ct.Name.Contains("(LtoD)"))
                                {
                                    continue;
                                }
                                else
                                    result.Add(ct);
                            }
                            rootFolder.UniqueContentTypeOrder = result;
                            rootFolder.Update();

                        }
                    }
                }

                if (!singleSite)
                {
                    SPWebCollection collWebsite = web.Webs;
                    foreach (SPWeb subSite in collWebsite)
                    {
                        SetDefaultContentTypeOnSite(subSite.Url, contentType, listName, singleSite);
                        subSite.Close();
                    }
                }
            }
        }
share|improve this answer
    
what does else if (ct.Name.Contains("(LtoD)")) stand for? –  Dennis G May 6 at 19:40

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.