# Setting file Content Type using Client Objects

Setting the Content type the same way as setting other metadata fields does not work in ClientObjects. Anybody knows why?

Here is what I do, and works for other columns:

var fci = new FileCreationInformation { Content = fileContent, Overwrite = overwriteIfExists, Url = serverRelativePath };
newFile.ListItemAllFields["someRandomColumn"] = "Hi, this works...";
newFile.ListItemAllFields.Update();
clientContext.ExecuteQuery();


What I tried, and got no reaction from SPS:

//Try to set it to a plain string:
newFile.ListItemAllFields[Constants.ContentTypeId] = ContentTypeIdInAString;

//Explicitly get the ContentType, and set it to the ContentType.Id:
var ct = spsLista.ContentTypes.GetById(ContentTypeIdInAString);
clientContext.Load(ct, x => x.Id, x => x);
clientContext.ExecuteQuery();
//this does retreive the expected ContentType object
System.Diagnostics.Trace.WriteLine("Setting Content type to: '" + ct.Id.ToString() + "'");
newFile.ListItemAllFields[Constants.ContentTypeId] = ct.Id;
//tried to set it to the full ContentType "ct" object, but no luck


Any ideas? Also, is there a good info source on using ClientObjects beyond the basics?

### Edit:

Thanks Vardhaman, your blog post is pretty close to what I'm doing, But still does not work. Good article, I will check out your other posts later...

But I'm puzzled: I could not change the content type on the sharepoint site itself for files uploaded using Client Objects. No error messages, no warnings, just the content type does not change. Editing in MS Word revealed, that there is a "ContentTypeId" custom property in the document itself (that did not show up among the "normal" editable metadata fields). After removing that property and checking in the file, I could change the content type manually on the SPS site. Needs some more investigation...

-
How did you guys know that the string is "ContentTypeId" ? Where is that information? Thanks –  ChiliYago Mar 25 '14 at 22:35
@ChiliYago: IIRC lot of trial-and-error, hacking and debugging around. Documentation for client objects is scarce, and most of it is "hello world" level, or along the lines of "works just like normal SPS development" (but actually doesn't...). :-/ –  Akos Lukacs Mar 27 '14 at 9:49
Thanks. I am painfully learning this. I am even working with an MS Engineer and it is taking hours to get our code doing basic manipulation of content types, list fields and security setting. ugh! –  ChiliYago Mar 27 '14 at 13:11

This should work:

newFile.ListItemAllFields["ContentTypeId"] = ct.Id.ToString();
newFile.ListItemAllFields.Update();
clientContext.ExecuteQuery();