Additional information: Unknown Error
Although it's rarely a requirement, I like to keep the "New Document" content types on my lists clean. Instead of merely adding any custom ones, (and configuring the list to allow this; see Code Listing 18 under The Script section of my book) I also remove those pesky out of the box options. In publishing, these include "Page," "Welcome Page," and "Article Page."
The thing we perhaps forget is that folders are content types in SharePoint, just like items and documents. Even though the "New Folder" functionality is separate, it's implemented the same on the backend as anything else we'd add to a list or library. Folders, like taxonomy, seem to have some special treatment in the API - meaning that what happens during list configuration on the browser is very different than what we'd see in the code.
For example, if you look at the content types associated with a list on the settings page, (listedit.aspx) you won't see "Folders;" that's controlled by a radio button on the advanced settings page (advsetng.aspx) for the list. However, when asking for all content types on a list in CSOM, Folder will be among them. So even though Folder is technically a content type, it's presented to developers and content authors in two very different ways.
This interesting dichotomy leads to the above error this post spotlights; namely, it occurs if you include Folder's ContentTypeId (which is 0x012000) among those assigned to a lists' UniqueContentTypeOrder in CSOM. This what I'm talking about: there's something "special" (hacky) going on here. Folder's good enough to be in the content type list (according to the API) but not good enough to be among the creatable content types (as far as the UI is concerned). So to avoid this, use the following code:
Code Listing 1
- public void SetContentTypeIds(string listName, List<ContentTypeId> ids)
- //open context
- using (ClientContext context = new ClientContext("http://sharepoint.local"))
- //load objects
- ll => ll.Include(
- l => l.Title,
- l => l.RootFolder,
- l => l.RootFolder.UniqueContentTypeOrder));
- //update list's content types
- List list = context.Web.Lists.SingleOrDefault(l => l.Title.Equals(listName, StringComparison.InvariantCultureIgnoreCase));
- list.RootFolder.UniqueContentTypeOrder = ids.Distinct().Except(ids.Where(id => id.StringValue.StartsWith("0x012000"))).ToArray();
After initializing the proper objects on Line #'s 7-12, the only interesting bit in SetContentTypeIds is Line #15. This is where we set the UniqueContentTypeOrder on the list, excluding Folder. Why we get an Unknown Error instead of something like "You cannot include Folder in the UniqueContentTypeOrder" or why it seems to work (or at least not bomb) in the server object model are all excellent questions I cannot answer. But if you're getting unknown errors in CSOM setting UniqueContentTypeOrder, this is first place I'd look.