Provision a Workflow to the Host Web from the App Web

below is some sample code to provision a workflow on the host web from your app web. This sample code is in one block for demo purposes. It makes 4 asynchronous calls to SharePoint to complete successfully.

In the event you’d want to define in your App a workflow that would run on a list in the host web this is the code you would use to deploy (move it from App to Host) and attach it to a list in the host web.

NOTE: you must include a reference to “/_layouts/15/SP.WorkflowServices.js”

this file can be downloaded here

   1: var attachWorkflow = function(workflowName, workflowListName, workflowHistoryListName, workflowTasksListName) {

   2:

   3:      //Using the App Web as the client context

   4:      clientContext = new SP.ClientContext.get_current();

   5:

   6:      //Get the host web URL from the query string params

   7:      //I have a function getHostWebUrl() - which is not included.

   8:      //http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript

   9:      var hostWebUrl = getHostWebUrl();

  10:

  11:      //Using the hostWebContext as an AppContextSite

  12:      hostWebContext = new SP.AppContextSite(clientContext, hostWebUrl);

  13:

  14:      //Get the Workflow Services Manager for the Host web, NOTE: you initialize it with the clientContext & the hostWebContext Web)

  15:      var hostWebWorkflowServicesManager = new SP.WorkflowServices.WorkflowServicesManager.newObject(clientContext, hostWebContext.get_web());

  16:      var hostWebWorkflowDeploymentService = hostWebWorkflowServicesManager.getWorkflowDeploymentService();

  17:      var hostWebSubscriptionService = hostWebWorkflowServicesManager.getWorkflowSubscriptionService();

  18:

  19:      //Get the Workflow Services Manager for the App web 

  20:      var workflowServicesManager = new SP.WorkflowServices.WorkflowServicesManager(clientContext, clientContext.get_web());

  21:      var workflowDeploymentService = workflowServicesManager.getWorkflowDeploymentService();

  22:      var workflowDeploymentServiceDefinitions = workflowDeploymentService.enumerateDefinitions(false);

  23:

  24:      //Load all the Workflow Definitions from the App web

  25:      clientContext.load(workflowDeploymentServiceDefinitions);

  26:      clientContext.executeQueryAsync(function () {

  27:

  28:          //Get the enumerator for all the workflow definitions in the App web

  29:          var workflowDefinitions = workflowDeploymentServiceDefinitions.getEnumerator();

  30:          while (workflowDefinitions.moveNext()) {

  31:

  32:              //set the current definition to a variable

  33:              var workflowDefinition = workflowDefinitions.get_current();

  34:              //test, trying to locate your workflow in the App web

  35:              if (workflowDefinition.get_displayName() === workflowName) {

  36:

  37:                  //now grab the xaml definition of the selected workflow

  38:                  var workflowXaml = workflowDefinition.get_xaml();

  39:

  40:                  //define a new workflow and assign it xaml & name

  41:                  var newWorkflow = new SP.WorkflowServices.WorkflowDefinition.newObject(clientContext, hostWebContext.get_web());

  42:                  newWorkflow.set_xaml(workflowXaml);

  43:                  newWorkflow.set_displayName(workflowName);

  44:

  45:                  //using the host webs Workflow Deployment Service, save the workflow.

  46:                  hostWebWorkflowDeploymentService.saveDefinition(newWorkflow);

  47:

  48:                  //load the new workflow and initialize the Id

  49:                  clientContext.load(newWorkflow, "Id");

  50:                  //this query will save your workflow to the host web

  51:                  clientContext.executeQueryAsync(function() {

  52:

  53:                      //publish the workflow on the host web

  54:                      hostWebWorkflowDeploymentService.publishDefinition(newWorkflow.get_id());

  55:

  56:                      //getting all the lists that the workflow will attach to

  57:                      //these lists already created on the host web

  58:                      var targetList = hostWebContext.get_web().get_lists().getByTitle(workflowListName);

  59:                      var historyList = hostWebContext.get_web().get_lists().getByTitle(workflowHistoryListName);

  60:                      var tasksList = hostWebContext.get_web().get_lists().getByTitle(workflowTasksListName);

  61:

  62:                      //loading the lists & initialize the Id

  63:                      clientContext.load(targetList, "Id");

  64:                      clientContext.load(historyList, "Id");

  65:                      clientContext.load(tasksList, "Id");

  66:

  67:                      //this query will publish the workflow and get the required list id's for the next step

  68:                      clientContext.executeQueryAsync(function() {

  69:

  70:                              //creating a new workflow subscription

  71:                              var subscription = new SP.WorkflowServices.WorkflowSubscription(clientContext, hostWebContext.get_web());

  72:

  73:                              //setting some properties of the subscription

  74:                              subscription.set_name(workflowName + "-ItemAdded");

  75:                              subscription.set_enabled(true);

  76:                              subscription.set_definitionId(newWorkflow.get_id().toString());

  77:                              subscription.set_eventSourceId(targetList.get_id());

  78:                              subscription.set_eventTypes(["ItemAdded"]);

  79:

  80:                              subscription.setProperty("TaskListId", tasksList.get_id().toString());

  81:                              subscription.setProperty("HistoryListId", historyList.get_id().toString());

  82:                              subscription.setProperty("FormData", "");

  83:

  84:                              //attaching the subscription to the target list 

  85:                              hostWebSubscriptionService.publishSubscriptionForList(subscription, targetList.get_id());

  86:

  87:                              //this query will execute the creation of the new subscription and adding attaching the subscription to the target list on the host web

  88:                              clientContext.executeQueryAsync(function() {

  89:                                  console.log("Workflow : \"" + workflowName + "\" successfully added to list : \"" + workflowListName + "\"");

  90:                              }, function(sender, args) {

  91:                                  console.log("Workflow : " + workflowName + " not successfully added to list : " + workflowListName);

  92:                                  console.log("<span style='color:red'>Reason : " + args.get_message() + "</span>");

  93:                              });

  94:                          },

  95:                          function(sender, args) {

  96:                              console.log("Failed to get workflow list IDs");

  97:                              console.log("<span style='color:red'>Reason : " + args.get_message() + "</span>");

  98:                          });

  99:

 100:                  }, function(sender, args) {

 101:                      console.log("Failed to create the workflow definition");

 102:                      console.log("<span style='color:red'>Reason : " + args.get_message() + "</span>");

 103:                  });

 104:              }

 105:          }

 106:      }, function (sender, args) {

 107:          console.log("Could not find workflow : " + workflowName);

 108:          console.log("<span style='color:red'>Reason : " + args.get_message() + "</span>");

 109:      });

 110:  };

Advertisements

8 thoughts on “Provision a Workflow to the Host Web from the App Web

  1. Hi,

    How to implement this to SharePoint-Host Apps? Event Handler is not support to SharePoint-Hosted Apps.

    Thank you.

    • This article is based on SharePoint hosted apps. To provision to the SharePoint site you would need to implement a configuration screen in your app.

      • Hi Thomas,
        Can I use above code to associate workflow from app web to host web on ONPREMISES SharePoint 2013. Seems everyone are talking about associating on SharePoint online only. Is there a way to associate a app workflow to already created host web in SharePoint ONPREMISES.

        Thank you

  2. I trying your code and it’s creating the workflow and association but not carrying over the initiation form. Is there something that needs to be set on the subscription object or elsewhere to set the workflow to first call the init form?

  3. Nice Article, Thanks for sharing.
    I want to know is it possible to deploy custom workflow activities from App web to hostweb?

    Thank you

    • Hi cv,

      Did you ever figure this out? It seems that if your workflows contain custom activities, you get an error trying to deploy the workflows from app web to host web. Do you know if there is a way of programmatically deploy the actvities to host web?

      Thanks

Comments are closed.