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.

Adresse Web Service - Del III - TDD i praksis

En af de almindeligste fejl i forbindelse med TDD er, at man midt i et "rød, grøn, refactor" forløb får en god idé til forbedring af designet og så straks begynder at implementere denne idé. Ergo man bliver ikke færdig med det igangværende forløb, samt at man begynder at implementere ny kode uden at skrive testen først.
Måden til at undgå dette er, at udvikleren løbende noterer idéer til nye funktioner/klasse på en TO-DO liste, og så holder fokus på det igangværende arbejde. Jeg prøvede 2-3 forskellige TO-DO programmer men for mig er løsningen nu noget så lavpraktisk som en kuglepen og en notesblok.

Codeplex' IssueTracker dækkede rigeligt behovet for at holde styr på feature requests, men samtidig var det absolut nødvendig, for den var medvirkende til at jeg holdt fokus på een feature af gangen, frem for at flagre fra det ene område til det andet.