Microsoft

Blog Categories

Subscribe to RSS feed

Archives

SharePoint 2010: Getting Workflows for a List From the JavaScript Client Object Model

All over the web, there are plenty of links to how to interact with SharePoint 2010 via the Client Object Model. Unfortunately, these interactions are all done via the compiled Client Object Model, not the Silverlight or JavaScript versions. That means you have to do the translation to JavaScript or Silverlight yourself. For Silverlight, that’s not particularly difficult, but keep in mind that the entire Silverlight object model is a subset of the Common Language Runtime (CLR). However, nowhere is there any information regarding how to interact with workflows from the Client Object Model, specifically from the JavaScript version. I bet you didn’t even know you could.

Fortunately, it’s not particularly difficult, provided you have the correct information at the time of processing for your script. All you really need is the GUID for the particular list (or site) that for which you want to get the associated workflows. From there, you get a ClientContext object and get the workflow associations. Something to keep in mind is that the Client Object Model is asynchronous for JavaScript, so you need to provide callback functions to execute when the service call succeeds or fails. Another important face is that the return values for the callbacks do not contain the values you requested. Instead the variables you asked to be populated are set. So be sure to keep them in scope when you need them in the callback functions. I find the best way to do this is inside of an object.

Without further ado, here is the necessary code, including callbacks:

 1: function loadWorkflows() {
 2:     //Get list Id from current page
 3:     var targetListGuid = SP.ListOperation.Selection.getSelectedList();
 4:
 5:     if (targetListGuid) {
 6:         this.guid = new SP.Guid(targetListGuid);
 7:         var context = SP.ClientContext.get_current();
 8:         var lists = context.get_web().get_lists();
 9:         var list = lists.getById(this.guid);
 10:
 11:         //Set workflows variable to load and be accessible in callbacks
 12:         this.workflows = list.get_workflowAssociations();
 13:         context.load(this.workflows);
 14:         context.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));
 15:     }
 16:
 17:     function onQuerySucceeded(sender, args) {
 18:         var enumerator = this.workflows.getEnumerator();
 19:         while (enumerator.moveNext()) {
 20:             var workflow = enumerator.get_current();
 21:             if (workflow.get_enabled() && workflow.get_allowManual()) {
 22:                 var workflowName = workflow.get_name();
 23:
 24:                 //Do something with workflow or workflowName
 25:             }
 26:         }
 27:     }
 28:
 29:     function onQueryFailed(sender, args) {
 30:         alert('Unable to retrieve workflows: ' + args.get_message());
 31:     }
 32: }

Once you’re inside onQuerySucceeded, you can call other functions and pass in this.workflows to call out from this function into whatever objects you want. Or you can do whatever processing you need to do.

Tags: , , ,

Leave a Reply