Technical Fun

Adding a Custom Content-Type Specific Item on a SharePoint List

Posted on: November 19, 2007

Let’s say I got a custom content type called “Boring Items” on my SharePoint list. It got some specific fields bound to it.
Now, I want to add an item to that list which will use the “Boring Items” content type. How?

First, perform the usual ritual of calling the SPSite and SPWeb thing, then find the list of your target, and grab its’ items [can be like this, but not necessarily has to be]:

SPSite site = new SPSite("http://YOUR_SITE_URL");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["YOUR_LIST_NAME"];
SPListItemCollection items = list.Items;

Then, bind it to the desired content type

SPContentType itemtype = list.ContentTypes["Boring Items"];
SPListItem newitem = items.Add();
newitem["ContentTypeId"] = itemtype.Id; //VOILA!
newitem.Update();

Continue adding the necessary field, and don’t forget to call the Update() method once you’re done.

Advertisements

1 Response to "Adding a Custom Content-Type Specific Item on a SharePoint List"

Thank you for the great post 🙂

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: