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

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...

share|improve this question
    
How did you guys know that the string is "ContentTypeId" ? Where is that information? Thanks –  ChiliYago Mar 25 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 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 at 13:11
add comment

1 Answer

up vote 1 down vote accepted

This should work:

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

Check out this link:

http://vangalvenkat.blogspot.in/2011/10/using-client-object-model-uploading-new.html

share|improve this answer
    
Thanks, it works. –  Akos Lukacs Oct 2 '12 at 8:33
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.