none
How to start workflow programatically created in SharePoint 2013 workflow platform?

    Question

  • Hello All,

    How to start workflow programatically created in SharePoint 2013 workflow platform in SharePoint Designer 2013. I don't need any initiation form to start this workflow so there is no initiation form created for it.

    Thursday, June 20, 2013 1:07 PM

Answers

  • Hi Rashic,

    Check this:

    var workflowServiceManager = new WorkflowServicesManager(web);
    var workflowSubscriptionService = workflowServiceManager.GetWorkflowSubscriptionService();
    //get all workflows associated with the list
    var subscriptions = workflowSubscriptionService.EnumerateSubscriptionsByList(listId);
    //run all workflows associated with the list
    foreach (var workflowSubscription in subscriptions)
    {    
       //initiation parameters    
       var inputParameters = new Dictionary<string, object>();    
       inputParameters.Add("MyProperty", "MyValue"); 
       workflowServiceManager.GetWorkflowInstanceService().StartWorkflowOnListItem(workflowSubscription, itemId, inputParameters);
    }

    For more detailed information, see

    http://ranaictiu-technicalblog.blogspot.com/2013/05/programmatically-start-sharepoint-2013.html

    Best Regards.


    Kelly Chen
    TechNet Community Support

    • Marked as answer by Rashi_C Saturday, June 22, 2013 7:40 AM
    Friday, June 21, 2013 2:57 AM

All replies

  • Hi Raschic,

    Below url might be helpful for you

    http://blogs.msdn.com/b/sumanc/archive/2008/08/16/starting-canceling-a-workflow-programmatically.aspx

    http://jaliyaudagedara.blogspot.in/2011/04/start-sharepoint-designer-workflow.html

    Regards  Roy Joyson
    Please remember to mark your question as "answered"/"Vote helpful" if this solves/helps your problem.


    Roy Joyson

    Thursday, June 20, 2013 6:04 PM
  • Hi Rashic,

    Check this:

    var workflowServiceManager = new WorkflowServicesManager(web);
    var workflowSubscriptionService = workflowServiceManager.GetWorkflowSubscriptionService();
    //get all workflows associated with the list
    var subscriptions = workflowSubscriptionService.EnumerateSubscriptionsByList(listId);
    //run all workflows associated with the list
    foreach (var workflowSubscription in subscriptions)
    {    
       //initiation parameters    
       var inputParameters = new Dictionary<string, object>();    
       inputParameters.Add("MyProperty", "MyValue"); 
       workflowServiceManager.GetWorkflowInstanceService().StartWorkflowOnListItem(workflowSubscription, itemId, inputParameters);
    }

    For more detailed information, see

    http://ranaictiu-technicalblog.blogspot.com/2013/05/programmatically-start-sharepoint-2013.html

    Best Regards.


    Kelly Chen
    TechNet Community Support

    • Marked as answer by Rashi_C Saturday, June 22, 2013 7:40 AM
    Friday, June 21, 2013 2:57 AM
  • Hello Roy,

    I have tried your above links, in that they are using "

    SPWorkflowAssociationCollection" in which 2010 workflow platform includes not of 2013 workflow platform. How to start workflow of SharePoint 2013 workflow platform?

    Friday, June 21, 2013 9:11 AM
  • Hi,

     Try the below link to start the workflow through SharePoint 2013 Workflow Services JS API.

    http://www.codeproject.com/Articles/607127/Using-SharePoint-2013-Workflow-Services-JS-API

     


    Balaji -Please click mark as answer if my reply solves your problem.

    Friday, June 21, 2013 10:24 AM
  • Hello Yaoyao Chen,

    I have used your above code, then SharePoint 2013 Workflow starts, but in list the Workflow 2013 status column shows blank but its running when I checked from Workflow report view page and after few seconds it shows Internal Status canceled automatically.

    And information for canceled it shows as:

    RequestorId: bae4b1d3-d0f7-38ca-2f6f-89a6d8063156. Details: System.ApplicationException: HTTP 401 The HTTP response content could not be read. 'Error while copying content to a stream.'. {"Transfer-Encoding":["chunked"],"X-SharePointHealthScore":["2"],"SPRequestGuid":["bae4b1d3-d0f7-38ca-2f6f-89a6d8063156"],"request-id":["bae4b1d3-d0f7-38ca-2f6f-89a6d8063156"],"X-FRAME-OPTIONS":["SAMEORIGIN"],"MicrosoftSharePointTeamServices":["15.0.0.4420"],"X-Content-Type-Options":["nosniff"],"X-MS-InvokeApp":["1; RequireReadOnly"],"Cache-Control":["max-age=0, private"],"Date":["Fri, 21 Jun 2013 11:16:28 GMT"],"Server":["Microsoft-IIS\/7.5"],"WWW-Authenticate":["NTLM"],"X-AspNet-Version":["4.0.30319"],"X-Powered-By":["ASP.NET"]} at Microsoft.Activities.Hosting.Runtime.Subroutine.SubroutineChild.Execute(CodeActivityContext context) at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager) at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor


    • Edited by Rashi_C Friday, June 21, 2013 11:24 AM added error message
    Friday, June 21, 2013 11:02 AM
  • I have find out that the above problem occurred because the workflow initiator is "system account". By default every workflow or the item created in the list from the visual studio solution is by "system account". So how to start SharePoint 2013 workflow platform programmatically with another user other then the system account?

    Saturday, June 22, 2013 5:25 AM
  • Hi,

    I used SPUserToken to start the workflow with other user, then workflow starts perfectly.

    Thansk all.

    Saturday, June 22, 2013 7:40 AM
  • Hi Rashic,

    Can you share the updated code?

    Thursday, July 04, 2013 2:41 PM
  • Hi Karol,

    Check the below code which I have used in my application.

    SPUserToken token = oweb.AllUsers["domain\\username"].UserToken;
    //create instance of WorkflowServicesManager
    elevatedSite = new SPSite(oweb.Site.ID, token);
    elevatedweb = elevatedSite.OpenWeb();
    SPUser user = elevatedweb.CurrentUser;
    // Perform administrative actions 
    var workflowServiceManager = new WorkflowServicesManager(elevatedweb);
    //find out the subscription (which actually represent the workflow association)
    var workflowSubscriptionService = workflowServiceManager.GetWorkflowSubscriptionService();
    //get all workflows associated with the list
    var subscriptions = workflowSubscriptionService.EnumerateSubscriptionsByList(WfList.ID);
    //check all workflows associated with the list
    foreach (var workflowSubscription in subscriptions)
    {
         if (workflowSubscription.Name == "TestWF2013")
         {
              //initiation parameters    
              var inputParameters = new Dictionary<string, object>();
              //inputParameters.Add("", "");
              workflowServiceManager.GetWorkflowInstanceService().StartWorkflowOnListItem(workflowSubscription, Wfitem.ID, inputParameters);
              Wfitem.Update();
              break;
          }
    }
    

    Friday, July 05, 2013 11:49 AM
  •    // ---------- 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(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) { 
              urlParams[key] = decodeURIComponent(value); 
            }); 
          } 
          return urlParams; 
        } 
      </script> 
     


    Friday, July 05, 2013 12:07 PM