25

Using some test code i found out that SPList.AddItem() executes slight faster then SPList.Add() - does anyone know why is it so ?

                DateTime start = DateTime.Now;
                SPList oList1 = oWeb.Lists["DemoList"];
                //SPListItemCollection oListCol1 = oList1.Items;
                SPListItem oItem1 = oList1.Items.Add();
                oItem1["Title"] = "11";
                oItem1["Created"] = new DateTime(2005, 3, 1);
                oItem1["Modified"] = new DateTime(2006, 3, 5);
                oItem1.Update();
                System.Console.WriteLine(DateTime.Now - start);

                start = DateTime.Now;
                SPList oList2 = oWeb.Lists["DemoList"];
                //oList2.AddItem();
                SPListItem oListItem3 = oList2.AddItem();
                oListItem3["Title"] = "12";
                oListItem3["Created"] = new DateTime(2005, 3, 1);
                oListItem3["Modified"] = new DateTime(2006, 3, 5);
                oListItem3.Update();
                System.Console.WriteLine(DateTime.Now-start);

OutPut 1 = 0.0322245 OutPut 2 = 0.0263655

2 Answers 2

34

This is related how instance of SPListItem is being created. Actually in both cases new item is added to the collection of items in list, in first case new item is added explicitly, in the second one implicitly.

The point is, that for initializing collection of items in list is used the same method GetItems but with different queries.

SPList.Items.Add()

In the first case

var newItem = list.Items.Add();

SPList Items property is initialized using the following query

new SPQuery()
        {
          ViewAttributes = "Scope=\"Recursive\""
        });

SPList.AddItem()

In the second case

var newItem = list.AddItem();

gets a collection of items from the list before new item is added based on the the following query

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

As you can see this is actually a fake query and returns no results at all,so it means items collection is empty when new item is added to it.

This is a principal difference to compared with the first query, where before new item is being added, items collection is initialized with items returned by query.


The execution time for these queries differ(second query is executed faster) and this affects to the creation time of new item.

3
  • 2
    good explanation. MS dev team told me to use AddItem as well in Sp 2010 and later.
    – Miles
    Commented Jan 24, 2013 at 17:11
  • Thanks, I updated the answer to specify more precisely, why in the second case the creation of a new element takes less time. This is mainly related with how initializing item collection happens. Commented Jan 25, 2013 at 12:13
  • 1
    Also keep in mind that you can't use the first approach when working beyond the threshold (default 5000 items for User / 10000 for Admins). For that reason (and performance) I tend to avoid list.Items at all.
    – Mx.
    Commented Feb 19, 2016 at 9:26
2

A little more background, AddItem() is the preferred method because as the list gets larger, it has to download all the items in the list when you call list.items.add() in order to add the one item.

This is why MS created the AddItem() call so that you could add an item without doing a query of all items in the list.

Avoid calling .Items off of a SPList object ever. When lists are small (like in development environments) all works well, but when it gets to production and the list fills up, the solution doesn't scale well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.