Start workflow SharePoint 2013 programmatically

Friday, July 5, 2013


Javascript :)  
   // ---------- Start workflow ---------- 
    function StartWorkflow() { 
      var errorMessage = "An error occured when starting the workflow."; 
      var subscriptionId = "", itemId = "", redirectUrl = ""; 
 
      var urlParams = GetUrlParams(); 
      if (urlParams) { 
        //itemGuid = urlParams["ItemGuid"]; 
        itemId = urlParams["ID"]; 
        redirectUrl = urlParams["Source"]; 
        subscriptionId = urlParams["TemplateID"]; 
      } 
 
      if (subscriptionId == null || subscriptionId == ""{ 
        // Cannot load the workflow subscription without a subscriptionId, so workflow cannot be started. 
        alert(errorMessage + "  Could not find the workflow subscription id."); 
        RedirFromInitForm(redirectUrl); 
      } 
      else { 
        // Set workflow in-arguments/initiation parameters 
        var wfParams = new Object(); 
 
        // get reviewer loginname 
        var html = $("#ctl00_PlaceHolderMain_docReviewerUser_upLevelDiv"); 
        wfParams['DocReviewerLoginName'] = $("#divEntityData", html).attr("key"); 
        // get editor loginname 
        var html = $("#ctl00_PlaceHolderMain_docEditorUser_upLevelDiv"); 
        wfParams['DocEditorLoginName'] = $("#divEntityData", html).attr("key"); 
 
        // Get workflow subscription and then start the workflow 
        var context = SP.ClientContext.get_current(); 
        var wfManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, context.get_web()); 
        var wfDeployService = wfManager.getWorkflowDeploymentService(); 
        var subscriptionService = wfManager.getWorkflowSubscriptionService(); 
 
        context.load(subscriptionService); 
        context.executeQueryAsync( 
 
            function (sender, args) { // Success 
              var subscription = null; 
              // Load the workflow subscription 
              if (subscriptionId) 
                subscription = subscriptionService.getSubscription(subscriptionId); 
              if (subscription) { 
                if (itemId != null && itemId != ""{ 
                  // Start list workflow 
                  wfManager.getWorkflowInstanceService().startWorkflowOnListItem(subscription, itemId, wfParams); 
                } 
                else { 
                  // Start site workflow 
                  wfManager.getWorkflowInstanceService().startWorkflow(subscription, wfParams); 
                } 
                context.executeQueryAsync( 
                    function (sender, args) { 
                      // Success 
                      RedirFromInitForm(redirectUrl); 
                    }, 
                    function (sender, args) { 
                      // Error 
                      alert(errorMessage + "  " + args.get_message()); 
                      RedirFromInitForm(redirectUrl); 
                    } 
                ) 
              } 
              else { 
                // Failed to load the workflow subscription, so workflow cannot be started. 
                alert(errorMessage + "  Could not load the workflow subscription."); 
                RedirFromInitForm(redirectUrl); 
              } 
            }, 
            function (sender, args) { // Error 
              alert(errorMessage + "  " + args.get_message()); 
              RedirFromInitForm(redirectUrl); 
            } 
        ) 
      } 
    } 
 
    // ---------- Redirect from page ---------- 
    function RedirFromInitForm(redirectUrl) { 
      window.location = redirectUrl; 
    } 
 
    // ---------- Returns an associative array (object) of URL params ---------- 
    function GetUrlParams() { 
      var urlParams = null; 
      if (urlParams == null) { 
        urlParams = {}; 
        var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gifunction (m, key, value) { 
          urlParams[key] = decodeURIComponent(value); 
        }); 
      } 
      return urlParams; 
    } 

C#

var WSM = new WorkflowServicesManager(web);
var wfSubscriptionService = WSM .GetWorkflowSubscriptionService();
//get all workflows associated with the list
var subscriptions =  wfSubscriptionService .EnumerateSubscriptionsByList(listId);
//run all workflows associated with the list
foreach (var workflowSubscription in subscriptions)
{    
   //initiation parameters    
   var inputParameters = new Dictionary();    
   inputParameters.Add("WFProperty", "Value"); 
   WSM .GetWorkflowInstanceService().StartWorkflowOnListItem(workflowSubscription, itemId, inputParameters);
}

Reference :
http://code.msdn.microsoft.com/office/SharePoint-2013-Approval-f5ac5eb2
 
share this post
Share to Facebook Share to Twitter Share to Google+ Share to Stumble Upon Share to Evernote Share to Blogger Share to Email Share to Yahoo Messenger More...

1 comments:

Unknown said...

Hi,

If I try to get an instance of WorkflowServiceManager() in a console application in an onpremise sharepoint infra. It throws an error -- cannot invoke method or retrieve property of null object. Object returned by following call stack is null.

The same csom code works fine with sharepoint online infra.

Do you have any idea on how to resolve this?