Find Out the Current Workflow State for an Item in Sitecore

Posted Thursday, February 14, 2008 2:06 PM by Kevin

Something I was working on today required me to figure out a way to determine the current workflow state for a Sitecore item.  Note, this code is running within the context of the content editor (a custom field type, to be exact).  Obviously, an item in the web database had better be in the "Published" state of the workflow. :)

As usual, digging around on the SDN5 site didn't help me out much, but eventually I figured this out:

Database masterDatabase = Factory.GetDatabase("master");
Item currentItem = masterDatabase.Items["/sitecore/content/Home"];
IWorkflow workflow = masterDatabase.WorkflowProvider.GetWorkflow(currentItem);
WorkflowState state = workflow.GetState(currentItem);

In this code, I'm getting a reference to the master database (since I'm working with an unpublished item), getting a reference to my site's homepage (just an example - in my real world code I already have an item reference), and then using GetWorkflow() and GetState() methods I found whlie exploring the Sitecore APIs.

In any case, this seems to work for me.  In my case, I need to know if the item is in the last state in the workflow, so I am checking if state.FinalState is true.

 

Comments

No Comments