Jason Specland: Consultant, Comedian

Making it up as I go along. Always.

Sharepoint: Updating Workflow Task Causes NullReferenceException (a fix)

I’ve got a workflow where a project manager approves a project (that’s one kind of task), or can send it out for technical review (another kind of task), or can request more information from the user (yet another task). Each of these tasks have their own Content Type, and their own custom task edit form in ASP.NET. (Because I hate InfoPath with a burning white hot hate.)

Whenever someone uses one of these custom edit forms, however, I get a NullReferenceException. The stack trace always puts it in the non-public SPListItem.PrepeareItemForUpdate method. It doesn’t matter how I change the task list item. Using SPWorkflowTask.AlterTask, or just SPContext.Current.ListItem followed by Update all threw the NullReferenceException.

To Google I go! However, most of the results said that it had to do with not having a definition in the Content Types. That wasn’t my problem, however, since not only did my Content Types have FieldRef’s, but they were very much full.

At my wits end, I broke out the .NET Reflector and decompiled the Microsoft.SharePoint assembly. Here’s what I found:

As you can see (well, kind of see… click on it to see more clearly), this would only happen in a WorkflowTask. If the list item’s “Completed” field is null, the cast to bool throws a NullReferenceException. This is probably the kind of thing that can’t happen in SharePoint Designer, but only would occur when someone is mucking about in Visual Studio.

To fix it, make sure you set the “Completed” field. I do this:

[sourcecode language=’c#’]
private void SetTaskStatus(string status, bool taskIsComplete) {
SPListItem taskItem = SPContext.Current.ListItem;

Hashtable taskData = new Hashtable();
taskData[SPBuiltInFieldId.TaskStatus] = status;
taskData[SPBuiltInFieldId.Completed] = taskIsComplete;
if (taskIsComplete) {
taskData[SPBuiltInFieldId.PercentComplete] = 1;
}

SPWorkflowTask.AlterTask(taskItem, taskData, true);
}
[/sourcecode]

Or, if you work for Microsoft, you could do us a favor and make sure that you null check that line and default to false. (It’s entirely possible that this has been fixed by SP1, but my production server doesn’t run that yet, so I can’t either.)

3 Comments

  1. Jason,
    You saved my day!!!!! Thank you for this post!

  2. After hour of time wasted, this fixed it!! Thanks for taking the time to post this info!

  3. Hi Jason,
    After spending whole day in this I referred you blog and got a solution. Thank you very much for your post. It realy helps lot.

Leave a Reply