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

It would appear that the Client Object Model only retrieves user-defined content types as part of the ContentTypes property for a given web. I would like to create a new content type using Document as a base, but I can't seem to make that work:

ClientContext ctx = new ClientContext("http://server/sites/site/");
// load web object
Web web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery();
// load ContentTypes
ContentTypeCollection types = web.ContentTypes;
ctx.Load(types);
ctx.ExecuteQuery();

ContentTypeCreationInformation info = new ContentTypeCreationInformation {
Name = "Custom Document",
Description = "Base type for all Custom docs",
Group = "Custom"
};
ContentType t = types.Add(info);
ctx.ExecuteQuery();

This is successful in creating the type, but it derives from Item - so I tried adding in the following for the ContentTypeCreationInformation but it gets effectively ignored... the content type still uses Item as the base. I am guessing it is because I never called Load() and ExecuteQuery() after types.GetById().

ParentContentType = types.GetById("0x0101")

Trying to load that content type by hand doesn't work either - I get a ServerException: Cannot invoke method or retrieve property from null object. Object returned by the following call stack is null. "GetByIdContentTypes"

ContentType doc = types.GetById("0x0101");
ctx.Load(doc);
ctx.ExecuteQuery();

Is there a way to create a Content Type using the Client Object Model where I can actually use Document as the base type?

Update 1

I created a content type using the web interface (ctypenew.aspx) that correctly inherits from the Document type. The following code will actually work under this crazy scenario, though I don't love having to create it via the web interface first and look up the ID.

// retrieve stub type created through web interface and get parent type
ContentType doc = types.GetById("0x0101002A430B08213A894C9D23FA4FD6DB068E");
ctx.Load(doc);
ctx.ExecuteQuery();
ContentType parent = doc.Parent;
ctx.Load(parent);
ctx.ExecuteQuery();

// now create new type using the parent reference
ContentType newType = types.Add(new ContentTypeCreationInformation { 
    Name = "Custom Document",
    Description = "Base type for all Custom types",
    Group = "Custom",
    ParentContentType = parent
});
ctx.ExecuteQuery();
share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

I just tried this code and it works as expected. After I run it I have a new content type named 'Test Document Content Type' that inherits from Document.

var clientContext = new ClientContext("http://win7virtualbox/sites/demo");
var web = clientContext.Web;

var contentTypes = web.ContentTypes;
clientContext.Load(contentTypes);
clientContext.ExecuteQuery();

var info = new ContentTypeCreationInformation();
info.Name = "Test Document Content Type";
info.ParentContentType = contentTypes.GetById("0x0101");
info.Group = "Pluralsight";

var newType = contentTypes.Add(info);
clientContext.ExecuteQuery();

Console.WriteLine("Done");

enter image description here

share|improve this answer
    
The above code doesn't work for me - I get a new Site Content Type named "Test Document Content Type" but the parent is still Item instead of Document. See image: i.imgur.com/8B1GB.png –  Goyuix Feb 22 '12 at 16:51
1  
The only think I can think of is that there is an issue with the content type id. Have you tried using the same code to create a content type that inherits from one of the other built-in content types (say Task) and see if that works? –  Rob Windsor Feb 22 '12 at 18:24
    
I tried a few other types as well (Like Basic Page and Picture) without success - it still defaults back to the base of item. I am convinced this must be something peculiar to my environment. Also see Update 1 in my original question as the mystery has expanded. –  Goyuix Feb 22 '12 at 20:03
add comment

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.