umbraco7umbraco-contour

Is it possible to stop/run Form workflow from a controller?


I am trying to achieve the below,

  1. When a link is accessed I want to approve/delete a record (with recordId and action type in querystring) by calling change record status workflow in a controller. Is it possible to execute a workflow from controller

  2. When a record is submitted, I want to check if the record with email already exists and stop form from saving new record in this case. I am hooking into the RecordInserting event to do some stuff but wondering if I could skip the record saving and Cancel/Stop all workflows from executing.

Thanks


Solution

  • To run a workflow from code, you need to access the WorkflowStorage class, which allows you to retrieve a workflow based on it's GUID. To do this, something like:

    var workflowStorage = new WorkflowStorage();
    var workflow = GetWorkflow(PUT YOUR GUID HERE);
    
    You can the run the workflow using the WorkflowService:
    
    var workflowService = new WorkflowService();
    
    var workflowList = new List<Workflow> {workflow};
    
    //NOTE, "e" is a recordEventArgs object
    workflowService.ExecuteWorkflows(workflowList, e);
    
    workflowService.DisposeIfDisposable();
    workflowStorage.Dispose();
    

    Should work. Populating the RecordEventArgs might require a bit iff trial and error though. The above code was taken from something that runs a different workflow depending on the values selected in a form, and runs inside another workflow, so e is already populated for you.

    The namespaces you'll need are:

    using Umbraco.Forms.Core;
    using Umbraco.Forms.Core.Enums;
    using Umbraco.Forms.Core.Services;
    using Umbraco.Forms.Data.Storage;