How to Call/Start a workflow Activity from Code

Topics: Customizing Orchard
Nov 4, 2014 at 3:33 PM
Is it possible to call a workflow activity that starts a workflow such as the ContentCreated activity from within the code? I am trying to build a workflow activity that starts a workflow whenever a user logs in. I implemented the IUserEventHandler in a custom class in my ExtendedWorkflows module and was thinking I could then call the workflow activity from within the LoggedIn event so the activity will fire whenever this event is fired. Thank you.
Nov 4, 2014 at 4:56 PM
Why do you want this behavior? I would only create a new Workflow an use the "Trigger"-Activity. Or do you want to hook up in the User_Created event?
Nov 4, 2014 at 7:02 PM
Can the "Trigger" Activity be used to start a workflow when a user logs in? I haven't really been able to figure out how the signal and trigger activities work.
Nov 4, 2014 at 7:26 PM
Hm, no I think it can be only trigger an event in an workflow - but not 100% sure.
Nov 4, 2014 at 10:08 PM
From your user event handler, you will need to trigger an event using the IWorkflowManager. Build a new UserLoggedIn activity (derived from Event), and trigger an event using this activity's name, and you're set.
Nov 4, 2014 at 10:22 PM
I just created an issue requesting some useful additional activities:
Please vote. :)
Nov 5, 2014 at 1:18 AM
Edited Nov 5, 2014 at 1:24 AM
I implemented a few Events for Users. You can take a look at
Nov 5, 2014 at 1:56 AM
Very nice. Thanks.
Nov 6, 2014 at 10:29 PM
Thank you, it worked for me.
Nov 7, 2014 at 4:18 PM
Edited Nov 7, 2014 at 4:45 PM
I would like to determine the role of the created user from within my UserCreatedActivity so I can return whether or not a user with the specified role was created instead of just returning any user was created like Rimango.Utilities currently does. Is it possible to somehow grab the created user and determine its roles from within the UserActivities.cs file? The trigger for the activity passes in the UserContext which should hold the user that was created, but I don't know how to access that.

_workflowManager.TriggerEvent("UserCreated", context.User.ContentItem, () => new Dictionary<string, object> { { "User", context.User.ContentItem } });

Any ideas would be greatly appreciated. Thank you.
Nov 7, 2014 at 11:09 PM
You should be able to get the roles from the user in the useractivity with something like that
var rules = workflowContext.Content.As<UserRolesPart>().Roles
But be sure that the User has a UserRolePart attached (standard User does have it - custom user maybe not).

But why want you do this in the UserLoggedInEvent? Use the UserLoggedIn-Event as startevent and then use the "IsInRole"-Activity to determine if the user is in the correct role.
Nov 7, 2014 at 11:13 PM
The problem with that is the roles activity gets the role of the current user that is logged in. I want to grab the roles for the user that was just created so I can trigger an activity if a user with the role administrator is created. I want to do this in the UserCreated event not the LoggedIn event.
Nov 8, 2014 at 2:13 AM
I see, that was my bad. Does it solve your problem so far or should I take a look at it?
Nov 9, 2014 at 7:36 PM
Yeah, that won't work either as there isn't an As function from within workflowContext.Content. I also tried workflowContext.Content.ContentItem, but that didn't work either.