Add New SharePoint List Items Efficiently

23 07 2009

Normally when adding new list items to a List using the object model you’d do something like;

	var aeList = spWeb.Lists[listName];
	var newListItem = aeList.Items.Add();

However, the drawback of this is that the OM will retrieve all of the List’s items (to populate the Items collection) prior to you calling the .Add() method. If your list has many list items, you’re probably going to start noticing a delay each time you add new list items.

A more efficient approach is shown below, effectively an empty query is performed against the list, and the .Add() method is called against the resulting (empty) SPListItemCollection.

	public static SPListItem EfficientAddItem(SPList list)
	{
		if (list == null) throw new ArgumentNullException("list");
		var nullQuery = new SPQuery { Query = "0" };
		return list.GetItems(nullQuery).Add();
	}




SharePoint Object Model Best Practices

14 04 2009

Found here on MSDN.