COM Again (0x81020037)
March 19, 2007
I found this "descriptive" error message in a fragment of MOSS 2007 code in a webpart that uploads and tags a document with appropriate metadata:
Microsoft.SharePoint.SPException: DOMAINuser has modified the file Sample.doc
System.Runtime.InteropServices.COMException (0x81020037)
The code 0x81020037 is "Save Conflict" error code in COM. The culprit:
SPListItem doc = list.Add();
doc\["Field1"\] = "Value";
doc\["Field2"\] = "Value";
doc.CheckIn("Check-in Message");
doc.Update(); <-- this is the line that throws the error
I tried reversing CheckIn and Update methods, with no success. However, the following trick did the job:
SPListItem doc = list.Add();
doc\["Field1"\] = "Value";
doc\["Field2"\] = "Value";
doc.CheckIn("Check-in Message");
doc = FindByTitle(doc.Title)
doc.Update();
FindByTitle is a custom method I wrote that finds a SPListItem in a SPList, performing a search on the Title field.
The explication: the underlying COM scaffolding doesn't like the Update() method after CheckIn(). I had to "reacquire" the item and perform the Update() on this new reference to the same document.
Elementary, dear Watson....