>
Blog
Book
Portfolio
Search

1/10/2014

9271 Views // 0 Comments // Not Rated

SharePoint 2013 Quick Tips: Unknown Error When Setting A List’s UniqueContentTypeOrder In CSOM

The title of this post says it all: sometimes, setting the UniqueContentTypeOrder on a List in CSOM barfs:

An unhandled exception of type 'Microsoft.SharePoint.Client.ServerException' occurred in Microsoft.SharePoint.Client.Runtime.dll

 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."

New Document Ribbon Button Options

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

  1. public void SetContentTypeIds(string listName, List<ContentTypeId> ids)
  2. {
  3. //open context
  4. using (ClientContext context = new ClientContext("http://sharepoint.local"))
  5. {
  6. //load objects
  7. context.Load(context.Web.Lists,
  8. ll => ll.Include(
  9. l => l.Title,
  10. l => l.RootFolder,
  11. l => l.RootFolder.UniqueContentTypeOrder));
  12. context.ExecuteQuery();
  13. //update list's content types
  14. List list = context.Web.Lists.SingleOrDefault(l => l.Title.Equals(listName, StringComparison.InvariantCultureIgnoreCase));
  15. list.RootFolder.UniqueContentTypeOrder = ids.Distinct().Except(ids.Where(id => id.StringValue.StartsWith("0x012000"))).ToArray();
  16. //save
  17. list.RootFolder.Update();
  18. list.Update();
  19. context.ExecuteQuery();
  20. }
  21. }

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.

No Tags

No Files

No Thoughts

Your Thoughts?

You need to login with Twitter to share a Thought on this post.


Loading...