다음을 통해 공유


How To: Access item metadata from workflow when associated in document library

Problem

There is a workflow which accesses the item metadata on workflow activated. In case of document library, when you upload a document, if the workflow is set to start on ItemAdded, it will start just after upload is done and before you completely fill up the metadata information of the item. Since it is starting before metadata is available and it is trying to access it, it fails with null reference exception.

Resolution

We found that when a workflow is associated with a doc lib to start on item created then it would start before all the metadata is entered while uploading a document. This is by design behavior and reason behind it is to cover all the document library’s functionalities, including multiple uploads.

An event handler can be associated with a library and from there we can initiate the workflows.
Workflows can be configured to start on item updated.

You can find out the associated workflow instances from document library’s object and then workflow template and workflow associations.

You can attach an event handler to the document library from your configuration screen and you need not to reset IIS for that.
Also,
while associating workflow to a doc lib, you can execute the following code.
            SPBaseType theBaseType = this._sharePointListAttachedTo.BaseType;
            if (theBaseType == SPBaseType.DocumentLibrary)
            {
                this._paramAutoStartWF_OnItemCreate = false;
            }
I noticed the changed event always fires on an upload in a document library. With the code above I set the OnItemCreate to false so only the change event fires and the workflow seems to work fine.