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.