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();