Friday, October 23, 2009

Adding a list item more efficiently

Scenario:
For one of our requirement we were suppose to add 100s of item to multiple lists and we got into some performance issues.

Solution:
Here's a workaround to avoid the overhead of calling SPList.Items.Add() method.

SPList.Update() must be called to save the newly created item.

Code:

public static SPListItem AddItem(this SPList list){

SPQuery query = new SPQuery()
{
Query = "<Where><Eq><FieldRef Name=\"ID\"/><Value Type=\"Integer\">-1</Value></Eq></Where>",
ViewFields = "<FieldRef Name=\"ID\"/>"
};
return list.GetItems(query).Add();
}

0 comments: