Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CQ5 Programmatically Run a Workflow

I created a workflow, in CQ, that needed to be run programmatically via XHR.

As many of you may know, CQ Documentation isn't the greatest (at least for this point). How can I run it programmatically ?

like image 910
Woodifer Avatar asked Mar 05 '13 20:03

Woodifer


People also ask

What is workflow session in AEM?

The WorkflowSession class provides all functionality (depending on the users rights) for managing WorkflowModel s, Workflow instances and their execution. It provides methods to: deploy/create a workflow models. start, stop, suspend, resume and complete a workflow instance.


1 Answers

After poking around for a while I wrote a servlet that runs a Workflow Model.

Here is the code with comments:

@Component
@Service
@Properties({
    @Property(name = "sling.servlet.paths", value = "/bin/runmodel"),
    @Property(name = "sling.servlet.methods", value = "GET")
})
public class RunWorkflowModel extends SlingSafeMethodsServlet {

    static private final Logger log = LoggerFactory.getLogger(RunWorkflowModel.class);

    @Reference
    private WorkflowService workflowService;

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
        ResourceResolver resourceResolver = request.getResourceResolver();
        Session session = resourceResolver.adaptTo(Session.class);

        /* Get Parameters
         * @param path = path you want to run the workflow on
         * @param model = workflow model name you want to run.  Typically found in /etc/workflow/models
         */
        RequestParameterMap params = request.getRequestParameterMap();
        String path = params.getValue("path").getString();
        String model = params.getValue("model").getString();

        // Create a workflow session 
        WorkflowSession wfSession = workflowService.getWorkflowSession(session);
        try {
            // Get the workflow model
            WorkflowModel wfModel = wfSession.getModel(model);                
            // Get the workflow data
            // The first param in the newWorkflowData method is the payloadType.  Just a fancy name to let it know what type of workflow it is working with.
            WorkflowData wfData = wfSession.newWorkflowData("JCR_PATH", path);
            // Run the Workflow.
            wfSession.startWorkflow(wfModel, wfData);
        } catch (WorkflowException ex) {
            response.getWriter().write("failed");
            log.error("Error starting workflow.", ex);
        }

        response.getWriter().write("success");
    }
}

Here is the Ajax call

CQ.Ext.Ajax.request({
    url: "/bin/runmodel",
    method: "GET",
    params : { 
        "path"  : "/content/path to item you want the workflow run on",
        "model" : "/etc/workflow/models/name of model/jcr:content/model"
    },
    success: function() {
        console.log("success");
    },
    failure: function(response) {
        CQ.Notification.notifyFromResponse(response);
    }
});
like image 190
Woodifer Avatar answered Sep 29 '22 12:09

Woodifer