søndag, august 02, 2009

Updating a SPListItem on Item Added in a Document List

Yesterday a colleague of mine at Flowit was working on an Event Handler in a Document Library, and the idea was to add some information from the site to the SPListItem when a new document was added.
Since there was no need for validation it sure looked like the event to use should be the ItemAdded event, but then all hell broke loose.
When calling Item.Update() SharePoint threw an exception :
Microsoft.SharePoint.SPException: Document Locked
The document you are attempting to edit is locked and cannot be updated.

Since this happened in the ItemAdded event we had to assume that the sequence was something like:
  1. Create new SPListItem
  2. Attach document to the SPListItem
  3. Release document
We checked whether the AttachmentAdded event was triggered but is was not.
We found a posting from Jan Tielens (U2U) from back in 2004, but the solution he used was more like a hack so we really didn't liked it.

Some time ago I read some of the documentation for SPList and noted the description for SPList.SystemUpdate() :

Updates the database with changes made to the list item, without effecting changes in the Modified or Modified By fields.

Back in the day I worked a lot with systems based on optimistic concurrency control and the only reason for SystemUpdate() that I could see, was to bypass the concurrency control!!!!

But in this case that bypass proved to be very handy, as it solved the issue:
call .SystemUpdate() rather then .Update() and the ListItem is updated as planned.

1 kommentar:

Anonym sagde ...

Tak for de interessante oplysninger