Efficient way to add a new item to a SharePoint list

Never use SPList.Items.Add because this approach gets all items in the list before adding a new SPListItem.  Use the following method instead, which does not preload the list items:

   1:  /// <summary>
   2:          /// More efficient way of adding an item to a list.
   3:          /// </summary>
   4:          /// <remarks>
   5:          /// GetItems with a query is faster than calling the OM to get all items.
   6:          /// This is because the SPListItemCollection is created without loading all the
   7:          /// data until the first query request.  Whereas SPList.Items loads all the data
   8:          /// at construction.
   9:          /// </remarks>
  10:          /// <param name="list">List.</param>
  11:          /// <returns>List Item Added.</returns>
  12:          public static SPListItem OptimizedAddItem(SPList list)
  13:          {
  14:              const string EmptyQuery = "0";
  15:              SPQuery q = new SPQuery {Query = EmptyQuery};
  16:              return list.GetItems(q).Add();
  17:          }
About these ads

11 Comments

  1. Posted March 2, 2009 at 9:51 am | Permalink

    Got any metrics on that? I can see what you’re saying, but I was wondering if you’d built a console app or anything to generate some timings. It seems a decidedly non-intuitive way of adding a list item!

  2. http://
    Posted March 2, 2009 at 2:08 pm | Permalink

    The only problem that I see with your approach is that this will fire any alerts, list handlers and workflows (that are programmed to fire automatically on create new or update list item events).

    I guess any developers planning on using the above approach should handle the issues that may result.

  3. Posted March 3, 2009 at 2:02 pm | Permalink

    Andy,

    I don’t have metrics to hand, but I did perform a study when I worked with a client, and the optimized version made significant difference in situations where we added lots of list items to lists.

    The bigger culprits are SPList.Items.Count, which I see all the time (especially in list iterators), which can be replaced with SPList.ItemCount.

    R.

  4. Posted March 3, 2009 at 2:03 pm | Permalink

    Saurabh,

    Quote me if I’m incorrect, but won’t SPList.Items.Add also fire events?

    R.

  5. http://
    Posted March 3, 2009 at 5:40 pm | Permalink

    Hi Rob,

    SPList.Items.Add will fire events but in that case hopefully all the necessary column fields will be populated (e.g. Title), so any alert or workflow that get created will have all the information they need.

    The only reason I bring it up is because in my experience, with such an approach email alerts will still be sent out without any information (other than the List Item ID). So it is something to be aware of.

    ~

    P.S. – Nice blog. It is very well organized and has extremely useful content.

  6. Posted March 3, 2009 at 5:43 pm | Permalink

    Ah! So that’s why they have them! Always did wonder why the separate ItemCount – always figured it was just a shortcut… …but it makes sense now!

    Methinks I’ll have to go tell the rest of the team…

  7. Posted June 17, 2009 at 12:28 pm | Permalink

    Some comments on MSDN’s SharePoint Best Practices page were complaining that the SPListItem.AddItem() method was missing.

    http://msdn.microsoft.com/en-us/library/bb687949.aspx

    I let them in on your helpful code here (re-cast as an extension method). Thanks for making it available.

    Cheers!

  8. Joseph Ackerbuch
    Posted November 4, 2009 at 9:56 am | Permalink

    The syntax of
    SPQuery q = new SPQuery {Query = EmptyQuery};
    does not work in Visual Studio 2005 and .NET2.0
    How is the correct syntax for those VS2005?
    Thanks.

  9. Posted November 4, 2009 at 2:27 pm | Permalink

    SPQuery q = new SPQuery();
    Query = EmptyQuery;

    Object inline initialization is part of .NET 3.0 and Visual Studio 2008.

  10. TazD
    Posted April 8, 2011 at 3:41 am | Permalink

    Very helpful post. Thanks.
    We are using this solution, but we’ve noticed a problem.

    The case:
    OwnFolder is custom type inherited from SPFolder.
    ItemUpdated event handler is attached to OwnFolder’s ItemUpdated. We want on moderation status change (inside ItemUpdated event) to do something to the items inside the folder. But when the folder was created using the AddItem on moderation status change the code inside ItemUpdated is not executed. All other events are executed correctly except this one.
    We have verified that when the folder is created through list.Items.Add(..) the problem is not verified.

    How we create:
    Creating SPFolder using AddItem and changing the content type to OwnFolder. Like this:
    SPListItem item = GetEmptyItems(list).Add(folderUrl, fsoType, fsoName);
    item.SystemUpdate(false);
    item[SPBuiltInFieldId.ContentTypeId] = OwnFolderContentTypeId;
    item.SystemUpdate(false);

    I hope this helps someone as I haven’t found any info on this problem anywhere.

  11. Posted May 27, 2013 at 11:13 am | Permalink

    Nice trick. Also, do not forget to set the RowLimit=1.
    Cheers.

5 Trackbacks

  1. [...] can look for more info about adding list item efficiently in this article by Rob [...]

  2. [...] Credit due for OptimizedAddItem(): http://blog.robgarrett.com/2009/02/25/efficient-way-to-add-a-new-item-to-a-sharepoint-list/ [...]

  3. [...] it sounds weird, but it’s true – it’s more efficient to run an empty query to get an SPListItemCollection to add the new SPListItem to, than to use SPList.Items.Add(). [...]

  4. By List Item Add Using SPQuery « Sladescross's Blog on December 14, 2010 at 11:15 am

    [...] it sounds weird, but it’s true – it’s more efficient to run an empty query to get an SPListItemCollection to add the new SPListItem to, than to use SPList.Items.Add(). [...]

  5. By Error Logging in SharePoint « SPMatt on February 6, 2012 at 9:26 am

    [...] I find it interesting that I have yet to see an implementation of exception logging to a SharePoint list. It’s a highly visible and accessible location to monitor issues within a SharePoint site. Another plus to logging to a SharePoint list is that it is sandbox-friendly and hence a viable option for SharePoint 365. Timmy Gilissen identifies this in his post SharePoint 365 logging and the example is not restricted to just sandboxed solutions (note that if you’re going to take the code from that page you may also want to read Rob Garrett’s post Efficient way to add a new item to a SharePoint list). [...]

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 )

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

Follow

Get every new post delivered to your Inbox.

Join 293 other followers

%d bloggers like this: