Friday, 15 December 2017

SharePoint 2013: Start Workflow with JavaScript Client Object Model


I have a request to start workflow using client side code. Below is the code that fix my issue:

Code sample:

<button type="button" onclick="Startworkflow(); return false;">Start Workflow</button>

<script src="https://code.jquery.com/jquery-1.7.1.min.js"></script>

<script>

function Startworkflow()

{ LoadScripts(); }

function LoadScripts(){

 SP.SOD.executeFunc("sp.js", "SP.ClientContext" , function(){

  SP.SOD.registerSod('sp.workflowservices.js', SP.Utilities.Utility.getLayoutsPageUrl('sp.workflowservices.js'));

  SP.SOD.executeFunc('sp.workflowservices.js', "SP.WorkflowServices.WorkflowServicesManager", StartWorkflow);

 }) 

}





//Subscription id - Workflow subscription id

//list item id for which to start workflow. If site workflow, then send null for itemId

function StartWorkflow() {

 
   var subscriptionId = "f5fa7d47-52c0-48af-9728-acfad55731d2";

   var itemId = "";



   var ctx = SP.ClientContext.get_current();

   var web = ctx.get_web();

   var wfManager = new SP.WorkflowServices.WorkflowServicesManager(ctx, web);



   var subscription = wfManager.getWorkflowSubscriptionService().getSubscription(subscriptionId);

   ctx.load(subscription, 'PropertyDefinitions');

   ctx.executeQueryAsync(

       function (sender, args) {

           var params= new Object();

           //Find initiation data to be passed to workflow.

           var formData = subscription.get_propertyDefinitions()["FormData"];

           if (formData != null && formData != 'undefined' && formData != "") {

               var assocParams = formData.split(";#");

               for (var i = 0; i < assocParams.length; i++) {

                   params[assocParams[i]] = subscription.get_propertyDefinitions()[assocParams[i]];

               }

           }

           if (itemId) {

               wfManager.getWorkflowInstanceService().startWorkflowOnListItem(subscription, itemId, params);

           }

           else {

               wfManager.getWorkflowInstanceService().startWorkflow(subscription, params);

           }

           ctx.executeQueryAsync(

               function (sender, args) {

                   console.log("workflow start successfully");

               },

               function (sender, args) {
                 
                   alert('Failed to run workflow');

               }

           );

       },

       function (sender, args) {         

           alert('Failed to run workflow');

       }

   );

 }


As I have a site workflow, I am passing "itemId" parameter value null. You can pass your item id there.


Reference: http://ranaictiu-technicalblog.blogspot.in/2013/06/sharepoint-2013-start-workflow-with.html

2 comments:

  1. This is a useful SharePoint 2013 example demonstrating how to start a workflow from client-side JavaScript using the Client Object Model. The step-by-step implementation highlights important concepts such as loading SharePoint scripts, retrieving workflow subscriptions, passing initiation parameters, and handling asynchronous execution. These techniques are valuable for developers maintaining SharePoint applications and automating business processes.

    For developers who want to strengthen their JavaScript skills, understanding asynchronous operations, client-side APIs, and event-driven programming is essential when building interactive applications and integrating with existing systems. Javascript Online Training in India can be relevant for those looking to develop practical JavaScript programming skills.

    ReplyDelete
  2. A structured learning approach can also help developers build a stronger foundation in JavaScript syntax, functions, API integration, and debugging. These fundamentals remain useful when working with legacy platforms such as SharePoint 2013 as well as modern web applications. Javascript Online Course in India is another resource for learners looking to improve their JavaScript knowledge.

    ReplyDelete

SharePoint Classic Site Retirement: Your Plan to Avoid Last-Minute Chaos

Classic SharePoint has been on borrowed time for years. Microsoft has now put dates on the remaining pieces. Microsoft will disable creation...