Sharepoint: Updating Workflow Task Causes NullReferenceException (a fix)

Jul 11, 2011 16:03


Hi there, ordinary blog readers! Unless you’re a SharePoint developer, this entry isn’t for you. I’m putting this up to save future developers the head-against-wall-banging that I encountered while trying to fix this problem.

So, I’m kind of new to SharePoint, so naturally, the first thing I should be doing is writing a crazy insane workflow, right?

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:

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); }
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.)

Originally published at The Many Hats of Jason Specland. You can comment here or there.

programming, sharepoint, geek

Previous post
Up