Thursday, March 5, 2009

Handling State Machine Workflow – Part 2

Hi All,


I am back with state machine worklfow part 2.

Please refer Handling State Machine Workflow – Part 1 for previous reading.

Let's proceed with part2. Ok, As of now we have services attached to the WorkflowRuntime, its now time to start an instance of the workflow.

First thing we will do here is get the name of the workflow class and we have to start the workflow from code. When we start the workflow, we will get an instanceID of that.

First get the WorkflowInstance class object.

WorkflowInstance instance;

instance = workflowRuntime.CreateWorkflow(typeof(Workflow1));


here workflow1 is the class name of state machine workflow. Change it accordingly to your state machine workflow class name. This method is even common for sequential workflow as well.

And then finally call,

instance.Start();

If you now take out the StateMachineWorkflowInstance like this,

StateMachineWorkflowInstance objinstance =
new StateMachineWorkflowInstance(workflowRuntime, instance.InstanceId);


you will get an idea about the first state being in objinstance. You can see it from its CurrentStateName property.

now, here comes the interesting part. As i said in previous part that you need to preserve the instance id because later at any other page or form, you may want to take the state machine workflow to different state. So you need to take out the same instance and perform the next state on the same instance. So for that you need to save this instance so that even machine is shut down due to power failure or any other thing. Then also you have instance saved in the database. For this, the very important line is.

instance.Unload();

Once you do this, your instance will get preserved to the database as we have attached the persistence service to the workflowruntime. Thanks to this service we are able to save it in the database.

The moment you unload the instance, it will be unloaded and freed from memory and stored in database, so even your memory is also free now. It will not keep running this workflow.

See the data stored in the persistence database once you unload the instance. Single Row will get inserted with that instance GUID. I have many other workflow instance persisted.



I will later explain you how we can take out a existing workflow instance and move our state ahead of same instance.

3 comments:

Aravind said...

Hi Thanks for blogging

I have a problem in state machine..
I have a sharepoint list i wanna retrieve list item data in workflow...i can get that data in sequential...but not in state machine..i have getting value can't be null error...

Can pls clarify my problem ...Is state machine only for document library...Pls reply me

Thanks

Aravind

SharePoint Kings said...

Thanks aravind,

method is the same to retrieve data in sequential and state machine workflow.

so there might be some other problem like reference missing of web/site/list/hashtable or something else.

it will be helpful if you share your code.

Router Mill said...

First part is also interesting.




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com