by Nikhil

2014-11-26

In some Apps there are cases when you want to deploy SharePoint 2013 workflow on Host Web rather than app web. This article explains how to do it.

The basic premise is that we first create the Workflow inside AppWeb and on ‘AppInstalled’ event we will get the published workflow definitions from AppWeb and copy it to Host Web.

To get started, create a workflow in Visual Studio (inside the AppWeb). One you’ve done that, add this code to the App Installed Event Handler execute this code.

Get the WorkflowDefinations from AppWeb

using (ClientContext sourceContext = new ClientContext(AppWebURL))

    {
                sourceContext.Load(sourceContext.Web);
                sourceContext.ExecuteQuery();
                var srcwsm = new Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager(sourceContext, sourceContext.Web);
                sourceContext.Load(srcwsm);
                sourceContext.ExecuteQuery();

                var srcwds = srcwsm.GetWorkflowDeploymentService();
                var srcwis = srcwsm.GetWorkflowInstanceService();
                var srcwss = srcwsm.GetWorkflowSubscriptionService();
                sourceContext.Load(srcwds);
                sourceContext.Load(srcwis);
                sourceContext.Load(srcwss);

                 var wDefCollection = srcwds.EnumerateDefinitions(true);
                sourceContext.Load(wDefCollection);
                sourceContext.ExecuteQuery();
 Dictionary<string,string> definations = new  Dictionary<string,string>();
                foreach (WorkflowDefinition wdef in wDefCollection) -- here you will get all the workflow definations Inside Your APP
                {

definations.Add(wdef.Name,wdef.Xaml);
                        

                }
            }

now you have the workflow defination xml as string.

Now Publish this workflow in HostWeb using below Code

ClientContext targetContext = SPCommonFunctions.GetClientContextHost(HostWebURL);

                targetContext.Load(targetContext.Web);


                var tgtwsm = new Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager(targetContext, targetContext.Web);
                targetContext.Load(tgtwsm);


                var tgtwds = tgtwsm.GetWorkflowDeploymentService();
                var tgtwis = tgtwsm.GetWorkflowInstanceService();
                var tgtwss = tgtwsm.GetWorkflowSubscriptionService();
                targetContext.Load(tgtwds);
                targetContext.Load(tgtwis);
                targetContext.Load(tgtwss);
                var lists = targetContext.Web.Lists;
                targetContext.Load(lists);


var targetList = lists["TITLE"]; -- get the target list on which you want to publish workflow

          
                Guid definitionId = Guid.NewGuid();
                string wfName = string.Empty;

                Dictionary<string,string> workflowDefinations = GetWokrflowDefinations(); -- Here get Definatiuon from the Code above

                foreach (KeyValuePair<string,string> keyvalpair in workflowDefinations)
                {
                    wfName = keyvalpair.Key;
                    string wfXaml = keyvalpair.Value;
                    var workflow = new Microsoft.SharePoint.Client.WorkflowServices.WorkflowDefinition(targetContext);
                    workflow.Xaml = wfXaml;
                    workflow.DisplayName = wfName;
                    targetContext.Load(workflow);
                    tgtwds.SaveDefinition(workflow);
                    targetContext.ExecuteQuery();
                    tgtwds.PublishDefinition(workflow.Id);
                    targetContext.ExecuteQuery();


                    List targetList = <GetYourLIstHere>

                    var wSub = new Microsoft.SharePoint.Client.WorkflowServices.WorkflowSubscription(targetContext);
                    targetContext.Load(wSub);

                    wSub.Name = wfName;
                    wSub.DefinitionId = workflow.Id;
                    wSub.EventTypes = new List<string>() { "ItemUpdated" };
                    wSub.EventSourceId = targetList.Id;
                    wSub.SetProperty("HistoryListId", historyList.Id.ToString());
                    wSub.SetProperty("TaskListId", taskList.Id.ToString());
                    wSub.SetProperty("StatusColumnCreated", "1");
                    wSub.SetProperty("ListId", targetList.Id.ToString());
                    wSub.SetProperty("Microsoft.SharePoint.ActivationProperties.ListId", targetList.Id.ToString());
                    var wSubId = tgtwss.PublishSubscription(wSub);
                    targetContext.ExecuteQuery();
                }



 

About the author 

Nikhil