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 };
var newFile = spsLista.RootFolder.Files.Add(fci);
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...