SharePoint: Adding large amounts of items to a list (c#)

By DimitriC at February 24, 2010 12:49
Filed Under: Programming, SharePoint

Apparently, when you add an item to a list using SPList.Items.Add(…), SharePoint is clever enough to first get all the items in that list and then add the new item. When you need to add a large number of items to a list, this slows down performance. What you need to do is first execute a query saying you need an empty list (RowLimit = 0) and then add your item to it. Then SharePoint will return the List-item with no listItems in it.

 

   1: SPQuery query = new SPQuery {RowLimit = 0};
   2: SPListItem newItem = list.GetItems(query).Add();
   3: newItem[SPBuiltInFieldId.Title] = DateTime.Now.ToString();
   4: newItem.Update();

 

Many thanks to René Hézser’s blog to pointing this out!

Comments are closed