Geeks With Blogs
Amusingly MOSS ...It's funny how difficult some stuff is when it really shouldn't be

I came across this last week.  It took me entirely too long to get it fixed, considering how simple the resolution was.  It turns out that my understanding of how Workflow operates wasn't up to snuff.

Scenario

I have a workflow attached to a forms library, activating when a list item is modified.  The intent of the workflow is to modify one or more fields on a particular list item in this list, after it is edited by another process.  Each of these fields are modified in different methods, which are tied to different nodes in the workflow - in other words, I couldn't update them all at the same time with the same workflowProperties.Item.Update() call.

As the workflow ran, the first update to the list item (which happened to be a workflowProperties.Item.File.SaveBinary() call) worked perfectly.  However, once the workflow got to the second field it was attempting to update, I got the following error:

Microsoft.SharePoint.SPException: A file with the name myfile.xml already exists. It was last modified by SHAREPOINT\system on 21 Feb 2009 15:31:28 -0500.

Troubleshooting

I figured that the error wasn't lying to me, but I wasn't sure how was supposed to make the code "aware" of the newly updated list item for the next update call.  I tried all permutations and variations of the Update() and SaveBinary() with all their different overloads to no avail.

I then discovered a very important fact - when the workflow initially hydrates, it "fetches" the data stored in the workflowProperties object only once up-front, which means that if I am going to run workflowProperties.Item.Update() twice in a row (as I am doing in my workflow), I'm going to have to re-fetch the list item between Update() calls.

Solution

I added a property to my Workflow class (please forgive the flagrant use of VB.NET):

Private ReadOnly Property WorkflowItem() As SPListItem

Get

Dim library As SPDocumentLibrary

library = CType(workflowProperties.Web.Lists(workflowProperties.ListId), SPDocumentLibrary)

Return library.GetItemById(workflowProperties.ItemId)

End Get

End Property

From then on, instead of using workflowProperties.Item, I use myItem = Me.WorkflowItem (to force the fetch each time I need to get to the item), and then use myItem.Update().  The problem evaporated altogether.

References

Posted on Monday, February 23, 2009 11:36 AM SharePoint | Back to top


Comments on this post: Workflow SPListItem modified by SHAREPOINT\system

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Adam McKee | Powered by: GeeksWithBlogs.net