4

I am not able to create a list content type using the below snippet. It throws a ServerException with additional information :

The site content type has already been added to this list.

   var list =  clientContext.Web.Lists.GetByTitle("sometitle");
   var documentCT = clientContext.Web.ContentTypes.GetById("0x0101");
   clientContext.Load(list,l=> l.ContentTypes);
   clientContext.Load(documentCT);
   clientContext.ExecuteQuery();

   var test = new ContentTypeCreationInformation(){
        Name = "TestCT", ParentContentType =documentCT };

   list.ContentTypes.Add(test);

   list.Update();
   clientContext.ExecuteQuery();

Basically, I want to create a list content type whose parent is the "Document" CT.

2
  • when you check the list settings, is the content type already there or not?
    – ova
    Commented Mar 7, 2016 at 10:03
  • No , its not there... Please also note that when I don't provide the ParentContentType value in ContentCreationInformation, it executes successfully (but parent type is Item) Commented Mar 7, 2016 at 11:26

2 Answers 2

3

Basically, I want to create a list content type

Basically, that's your problem, because there is no such thing.

As opposed to columns that can be created on both list and site level, content types are always created on web level and later added to list.

This can be confusing for two reasons:

  1. In CSOM both Web and List contain property ContentTypes represented by the same class ContentTypeCollection. This is inherited from SSOM (SPWeb, SPList, SPContentTypeCollection respectively).
    In SharePoint SPContentTypeCollection has different behavior when it's parent is web and list. This is bad design and lazy programming done by SharePoint team as early as WSS 3.0. Now we need to work with API that sometimes behaves in unexpected way.
  2. Id of content type added to list suggests, it's a child content type.

We can create multiple content types with the same parent, but we can only add them once to a list. This behavior matches what you can do using UI. Try adding Document content type to document library - it's not even available in the picker.

Ok, ranting over. What you need to do is create it on web first:

var web = context.Web;
var list = web.Lists.GetByTitle("Documents");

var baseCT = web.ContentTypes.GetById("0x0101");
var creationInfo1 = new ContentTypeCreationInformation
{
    Name = "Document1",
    ParentContentType = baseCT
};
var creationInfo2 = new ContentTypeCreationInformation
{
    Name = "Document2",
    ParentContentType = baseCT
};

var childCT1 = web.ContentTypes.Add(creationInfo1);
var childCT2 = web.ContentTypes.Add(creationInfo2);

list.ContentTypes.AddExistingContentType(childCT1);
list.ContentTypes.AddExistingContentType(childCT2);
//You can also use list.ContentTypes.Add() method, but it takes few lines more

context.ExecuteQuery();

And yes, this is working code. There's no context.Load(). There's no Update(). Single ExecuteQuery() is all you need.

1
  • Yes, you are right... I can't do this via CSOM.. But I have found that creating list content type can be done via SSOM. The problem is the my target sharepoint is Sharepoint Online. So I can't possible use SSOM. Thats why I was wondering if adding list content type can be done via CSOM.. I guess not.. Anyways thanks for clearing that up .. Much appreciated :) Commented Mar 14, 2016 at 4:50
0

I think, you are trying the code in the subsite. In subsite if you want to access the content types, use the Web.AvailableContentTypes instead of Web.ContentTypes.

Try the below code, which can solve the issue.

   var list =  clientContext.Web.Lists.GetByTitle("sometitle");
   var documentCT = clientContext.Web.AvailableContentTypes.GetById("0x0101");
   clientContext.Load(list,l=> l.ContentTypes);
   clientContext.Load(documentCT);
   clientContext.ExecuteQuery();

   var test = new ContentTypeCreationInformation(){
        Name = "TestCT", ParentContentType =documentCT };

   list.ContentTypes.Add(test);

   list.Update();
   clientContext.ExecuteQuery();
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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