Add New SharePoint List Items Efficiently


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();
	}

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.