Properly Checking Metadata in SharePoint Workflow

Since you want your SharePoint workflow to be as fault tolerant as possible it is always a good idea to use the following method when you are accessing fields from an SPListItem. This is especially true if they are custom fields that are deployed via a Site Column or Content Type as your workflow will receive nasty errors such as “Null Reference Exception” and “Array Out of Bounds Exception” otherwise.

if (workflowProperties.Item.Fields.ContainsField("Custom Field") && workflowProperties.Item["Custom Field"] != null && workflowProperties.Item["Custom Field"].Equals("Some Value"))
{
// continue with rest of code
}
I'm a public speaker and the Chief SharePoint Architect for Eastridge, a Microsoft Gold Partner specializing in SharePoint and custom application development in Winston-Salem, NC. I focus on the SharePoint platform with a specialty in Information Architecture, Publishing and Best Practices.

Got something to say? Go for it!