Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 7025

Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 2162

Deprecated: Hook imagify_allow_picture_tags_for_webp is deprecated since version 2.2! Use imagify_allow_picture_tags_for_nextgen instead. in /www/collab365_296/public/wp-includes/functions.php on line 5758
How to create and publish a SharePoint 2013 Workflow from an App in the Host Web (without using SharePoint Designer) - Collab365
Deprecated: strstr(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1145

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1152

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1155

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1162

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1165

Deprecated: strstr(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1145

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1152

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1155

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1162

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1165

Warning: Undefined array key "url" in /www/collab365_296/public/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/smart-site/classes/class-tvd-smart-shortcodes.php on line 85

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();
                }



 

(Visited 107 times, 1 visits today)

About the author 

Nikhil

Summit Bundle

Get 200+ hours of Microsoft 365 Training for 27$!

Master Office 365, Power Platform & SharePoint & Teams With 200+ Hours Of Training Videos and 108 Ebooks in the Collab365 Academy. This offer is insane and is only available for a limited period.