0

I have a timer job that queries an external list, and then copies items into a regular SharePoint list. I store the ID of the external list item on the SP list item so I can update instead of add if appropriate.

In order to fully add an new item programmatically, you have to call list.Items.Add() to get a new SPListItem object, set field values on it, then call item.Update(). The only field I set during the initial add is the field that stores the ID of the external item. There are no required fields. There are no workflows on the SP list. I'm testing this on my Dev environment, and the SP list that is getting new items added is initially empty.

However, after calling item.Update(), it takes nearly exactly 5 minutes 30 seconds before code execution continues. I am logging, and I set up very explicit logging which is how I see this. Here's the method that does the adding (I turned the logging code into comments, but that's where the log entries are being made):

private SPListItem AddNewItem(SPListItem externalCTItem, SPList list)
{
    // Log: entering method

    SPListItem newItem = null;
    string externalItemID = externalCTItem["ID"].ToString();
    try
    {
        // Log: Calling list.items.add()
        newItem = list.Items.Add();

        // Log: Setting field on new item
        newItem["ExternalDB_ID"] = int.Parse(externalItemID);

        // Log: Calling newItem.Update()
        newItem.Update();

        // Log: New item created  <-- this gets logged around 5m 30s after the last log entry
    }
    catch (Exception ex)
    {
        // Log: exception & stacktrace
    }

    // Log: exiting method
    return newItem;
}

I then go into another method where I set a bunch of other fields, and call item.Update() again after that, and that all works fine, the big delay is only after the initial update that commits the newly added item.

Why would it take five minutes to add a new item to a list?

Oh, also want to add that if I go to the SP list, I can see the newly added item with the one field set. Which means the initial update actually worked, and quickly, it's just whatever happens at the end of item.Update() is taking forever.

1 Answer 1

3

Replace

newItem = list.Items.Add();

with

newItem = list.AddItem();

SPList.AddItem() method is much more faster especially while dealing with a List with large number of items.

Refer to: SPList.Items.Add() v/s SPList.AddItem()

4
  • In a nutshell, it is because list.Items.Add() does retrieve all list items before it adds a new item.
    – Salah
    Apr 26, 2019 at 6:10
  • @Salah yes, but that was my point in saying that I'm testing that on my Dev environment and the list that is getting things added to it initially is empty. The fact that list.Items.Add() retrieves all items should be irrelevant because initially there are zero items, and the first add still has the five minute problem. Apr 26, 2019 at 13:47
  • @LZ_MSFT I have read that AddItem is faster than Add, but my logging entries show that the time between when I log that I'm about to call Add and the next log entry that I'm about to set the field value is milliseconds. So it seems that the time it takes to call Add is negligible. The delay is after I call Update, not after I call Add. Please re-read my question. Apr 26, 2019 at 13:49
  • Also keep in mind my final comment - by going to the list in a browser, I can see that Update has been applied, I can see the new item in the list with the one field set to the correct value. However, even though the Update worked, code execution does not continue to the final logging line where I log that the item created successfully for 5m 30s. So the delay is not even necessarily with the Update itself, it is after the update, with whatever cleanup code finishes the Update method before returning execution to the caller. Apr 26, 2019 at 13:53

Your Answer

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

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