Sigge Persson


I am building my first statemachine and i have run in to a problem i have been unable to solve.

The workflow is simple.

It starts after a InfoPath form has been created and uses som of the data from it to create a task (another infopath form with a checkbox and a button). When the button is clicked a ontaskchanged event is supposed to be triggered to start the next task etc.

the first state consists of: CreateTask and SetState

that points to the second state that consists of an EventdrivenActivity that is supposed to catch the onTaskChnged event and an if/else for a loop back or completeTask/deleteTask/SetState to the next.


When i run the workflow it runs up to the point where it has moved to the second state and is waiting for the event. as i understand it it will idle then be saved and unloaded (automatically in sharepoint (as far as i have understood)).

the workflow is marked as "Completed" in the list, wich someone on this forum said meant that it IS completed and cant be started up again.

and that is exactly the problem i have. the task has been created but when i perform it the workflow still does not respond and the task itself is marked as "not started".

the correlation tokens are correct i.e. they are their own within the task (although OwnerActivityName is the workflow).

Is the problem that i dont have the event triggering the activity properly or is it that the workflow is "completed"

Been trying for a long time and i'm at my wits end.

thank you in advance.

Re: SharePoint - Workflow StateMachine task

Sigge Persson

I have now made another workflow where the task is created (in the StateInitializationActivity) in the same state as it is completed.

In the onTaskChanged method there is an if/else case where if the task is not completed no action will be taken and when its completed it will complete/delete the task and switch state.

the ownerActivityName of the Correlation token is now the state.

But i still get the same error.

Re: SharePoint - Workflow StateMachine task

Andy Burns

Unless you delete them, the tasks created by your workflows will continue to exist after your workflow is complete. Are you deleteing the tasks in the StateFinalisation event for the appropriate state It's not clear from your description.

Have you added an 'End state' for your workflow You probably want a State at the end that is just an 'end state'. Add a State to the design, right click on it and select the option that is something like 'Set as completed state'. (You should see a little Red icon in the state, similar to the little green icon in the starting state of the workflow). Then set up the transition to this state to happen when your workflow is complete.

I'm not sure I understand how you've structured your workflow, though. Typically, I would have a state for each Task. I would use a State Initialisation event to create the task, and a State Finalisation event to delete or complete the task. I'd have an EventDriven event handler to capture the OnTaskChanged event, where I would have logic about whether to go to a new state, and if so, which state. The correlation token for all of this would have an owner of the state itself (as the task will never be used outside of this one state).

I would repeat this for each task in my workflow. At the end, I would have a 'Workflow completed' state, created as mentioned above.

Re: SharePoint - Workflow StateMachine task

Sigge Persson

Yes i do delete the tasks but i never reach that code, since the workflow doesnt reactivate when i perform the task.

i dont delete them in the state finalizationactivity though, i do it in the if/else leg just before i set a new state.

I do have a endstate for my workflow, but the code never continues after creating the first task. it creates the task and then is flagged "completed"

My initial state only includes a OnWorkflowInitializedActivity (placed inside a EventDriven Activity without a condition for activation) together with a setState for my second state.

My second state consists of 2 activities. SateInitialization and EventDriven activity.

the SateInitialization includes and empty code snippet to be filled out later and a CreateTaskActivity.

*This is where the code stops and workflow gets flagged as complete.*

the EventDriven Activity includes a OnTaskChanged Activity to catch the event and a If/Else to see if the checkbox (in the task) is flagged or not.

if it is it has a TaskComplete , a TaskDelete, and a SetState formy third state (wich has almost the same look, only one more leg in If/Else for sending back to the second state) .

if it isnt checked it enters an empty leg (hoping it will wait for another OnTaskChanged event).

Re: SharePoint - Workflow StateMachine task

Sigge Persson

the workflow has now been rebuild to use both initialization and finalization as you suggested.

initialization: code & create task.

EventDriven: onTaskChanged & if/Else (one leg with SetState and the other empty)

finalization: completeTask & deleteTask

the workflow stops where it should wait for a task to be completed but is flagged as completed and nothing happens when the task is done.

Re: SharePoint - Workflow StateMachine task

Andy Burns

Strange. I don't know then. The only other thing I can think of is a fault in the correlation tokens used - but I wouldn't expect the workflow to complete then.

Re: SharePoint - Workflow StateMachine task

Chandler Chao

This may sound trivial but are your OnTaskChanged and CompleteTask actions all refering to the taskID field that you assigned to your CreateTask action Even though you have the correlation token set you still need them all to share that taskID so they know which item they need to work on.

Re: SharePoint - Workflow StateMachine task


I've had the same problem...
My problem was that I had a class which was not serializable in my code. So when the workflow went to sleep, it couldn't serialize this class and failed but was shown as "Completed". The solution was very simple: just put "[Serializable]" before my class...
Hope that was your problem !

Re: SharePoint - Workflow StateMachine task

Sigge Persson

I think that might be it (serializable), i will check that when i get in tomorrow.

thank you.

Re: SharePoint - Workflow StateMachine task

Sigge Persson

Unfortunatly that didnt help either.

Thanks for the tip though.

Re: SharePoint - Workflow StateMachine task

Sigge Persson

Ok, let me just ask questions that i think might have something to do with it.

Do i have to programmatically use SPWinOePersistenceService in some way for it to save the workflow (i ws under the impression it persists automatically). I just have "using Microsoft.SharePoint.Workflow;" atm.

do i have to do anything in code to bind the onchanged event than just set in the porperties :

InterfaceType Microsoft.SharePoint.Workflow.ITaskService

EventName OnTaskChanged

With the correct correlation token.

Cant think of much else myself.

Re: SharePoint - Workflow StateMachine task

Andy Burns

No, it should just persist all by itself. I've never needed to define anything as serializable for persistence to work.

Did you check Chandler's comment Are you using the same TaskID field for the create, update, and complete/delete tasks And are you assigning a new GUID to it in the createTask activity

I can provide an example of a working state machine workflow if that would help

Re: SharePoint - Workflow StateMachine task

Sigge Persson

yes, i do have the same task id on all parts of the same task ( a TaskID-field that is genereated through VS)

all tasks has "TaskXXId=Guid.NewGuid();" in the taskCreate method.

and a working StateMachine would be very appreciated.

Thank you very much.

I am getting quite frustrated, have been trying to get this to work a long time.

i found this on serialzable under the second pitfall.

if you want to contact me, you can find me on

Thanks in advance.

Re: SharePoint - Workflow StateMachine task

Chandler Chao

You know I was working on a state workflow yesterday when I had a similar problem. The workflow would enter the first state, the task would be created and then the whole workflow would mark as complete. There was no error thrown in the histroy at all. Couldn't figure out what it was until I removed the workflow from the list using the GUI and then re-attached it. It then started marking an error in the history, and it was something to the effect that an SPUserCollection that I declared as a class variable and assigned in OnWorlflowActivated isn't marked serializable, therefore it couldn't be persisted. So I moved the collection to a local method variable and everything started working correctly again.

So check your class objects. If you are using collections or other complex objects they may not be serializable.

Re: SharePoint - Workflow StateMachine task

Sigge Persson

I use SPUser and i instantiate a class that uses SPCollection in a codesection, but even if i remove them i get the same error.

I am trying to find anything else that might be of the same things, but it looks pretty clean to me now.

Unfortunatly i still have the same error.

I'll post the code in its emptiest form in the next post.

Thank you for the help.