Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 18 down vote accepted

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.

share|improve this answer
1  
good explanation. MS dev team told me to use AddItem as well in Sp 2010 and later. –  Miles Jan 24 '13 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. –  Vadim Gremyachev Jan 25 '13 at 12:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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