Thursday, October 23, 2008

System.InvalidOperationException: The event receiver context for Workflow is invalid.

If the below error come up while creating sharepoint worflow:

System.InvalidOperationException: The event receiver context for Workflow is invalid. at Microsoft.SharePoint.SPEventReceiverDefinition.ValidContext() at Microsoft.SharePoint.SPEventReceiverDefinition.ValidReceiverFields() at Microsoft.SharePoint.SPEventReceiverDefinition.GetSqlCommandToAddEventReceivers(IList`1 erds) at Microsoft.SharePoint.Workflow.SPWinOESubscriptionService.CommitNewSubscriptions(Transaction txn, IList`1 erds)


The cause of this is either you didn't associate properties for TaskID, TaskProperties, or CorrelationToken activity to your createtask token. The other scenario that this comes up is when you don't assign a unique id to your Taskid. If you code looks like below:
public Guid createTaskInitiation_TaskId = default(System.Guid);
to
public Guid createTaskInitiation_TaskId = Guid.Newguid();
This should resolve the error.

Monday, October 13, 2008

How to prevent code from crashing when adding an event handler to a documentlibrary.

I had the follwing issue:

I added an event handler to a document library. Every time i added a new document the eventhandler would run fine but when a user had entered metedata againt a doucument and pressed ok, sharepoint would crash. The reason behind this was, I was tring to update an item when there was already an update in progress. I fixed the issue by running the follwing update code.

DisableEventFiring();
item.SystemUpdate(false);
EnableEventFiring();