user318197
user318197

Reputation: 333

Could not change the content type of a Document list item in a document library

I'm programatically changing the content type of a document in a document library. But document is getting added with the content type only.

Here is my coding:

SPFile ofile = oweb.GetFile(oweb.Url + '/' + oDocSet.Item.Url + '/' + refName);
ofile.Item["Content Type"] = octype.Name;
ofile.Item["Content Type ID"] = octype.Id;
ofile.Item.Update();

when i traced the above coding the content type is getting changed when i assign them to the item but after I update the the item in the last line the content type of the item is getting set to the default content type.

Upvotes: 0

Views: 3230

Answers (2)

Michael
Michael

Reputation: 31

I had the same situation as described here. ContentType remains the same after save document in document library in custom form. Code provided above runs without exceptions and messages, but contenttype remains the same.

Problem solved with reverting back to default edit form, changing contenttype with it, and reassigning custom edit form in Sharepoint Designer again. Now contenttype changes work without problems.

Maybe it internal bug with sharepoint.

Upvotes: 0

Colin
Colin

Reputation: 10638

Shouldn't it be:

SPFile ofile = oweb.GetFile(oweb.Url + '/' + oDocSet.Item.Url + '/' + refName); 
ofile.Item["ContentType"] = octype.Name; 
ofile.Item["ContentTypeId"] = octype.Id; 
ofile.Item.Update(); 

Note the lack of spaces in the field names

P.S.: Is the content type actually allowed in the library?

Upvotes: 1

Related Questions