1

I set my library default ContentType as "A", then i want to change it by some rule if i use SystemUpdate() it can not change ContentType to B

  if (type== "A") {
      item["ContentTypeId"] = list.ContentTypes["A"].Id;
  } else if (type== "B") {
      item["ContentTypeId"] = list.ContentTypes["B"].Id;
  }
  ......
  item["something"]="text";
  ......
  item.SystemUpdate();
  • I'm having the same problem... – Nacht Aug 21 '15 at 1:21
0

You may need to set the content type name as well:

if (type == "A") {
    item["ContentTypeId"] = list.ContentTypes["A"].Id;
    item["Content Type"] = list.ContentTypes["A"].Name;
} else if (type == "B") {
    item["ContentTypeId"] = list.ContentTypes["B"].Id;
    item["Content Type"] = list.ContentTypes["B"].Name;
}
.......
item.SystemUpdate();

But, actually, looking at your code, if you have a variable called "type" that is an identifier of the content type you want to use, you don't even need the if..then..else statement:

........
// code in here that sets the value of "type"
// to be either "A" or "B"
........
item["ContentTypeId"] = list.ContentTypes[type].Id;
item["Content Type"] = list.ContentTypes[type].Name;
........
item.SystemUpdate();

That would set the content type based on the value of type, whether it's "A" or "B".

  • I found if i change ContentTypeId only then call item.SystemUpdate(), the item will change ContentType, if i do more things like change properties (like: item["XmlType"]="Old") then call item.SystemUpdate(), the ContentType will restore to default ContentType. At last i change the ContentType and call Update again after change all properties and call Update(), this could works. – wtotal Jul 11 '13 at 3:20
  • Ok, maybe it is changing the other properties that is causing the content type change to fail. I have successfully changed content types by changing the ContentTypeId and Content Type (name) properties only, and then immediately calling SystemUpdate(), before trying to set any other properties. So yes, maybe trying to change the content type only, calling an update, and then proceeding to change other properties, will work. – Dylan Cristy Jul 11 '13 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.