3

Using object model I need to add an item to a list. The list has 4 custom content types. Depending on data provided I need the item to be of a specific content type.

How is this done?

Using SPListItem oItem = list.AddItem(); does not accept a content type as a parameter. Neither can you set it later it seems the property is read only.

Any ideas?

  • I also have the same problem... can you please help me if you found the solution for that... thanks... – user4928 Sep 28 '11 at 16:54
  • user4928 - if you have a simliar problem that my answer does not solve, then please create a new question and explain why my answer isn't relevant. The answer to your problem may help others! :) – James Love Sep 28 '11 at 22:06
7

Use SPList.Items.Add() to return the item you're adding (as an instance of SPListItem), instead of SPList.AddItem().

Then you can update the ContentTypeId using myListItem["ContentTypeId"] to set the content type.

Here's an example: http://asadewa.wordpress.com/2007/11/19/adding-a-custom-content-type-specific-item-on-a-sharepoint-list/

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.