Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Requirement
In this article we will be exploring a solution to the one of the most obvious problems with SharePoint Designer Workflows i.e. copying the SharePoint Designer Workflow from one site to another.
Solution
In order to set the demo environment, I have got one site created called “Development” as shown below:
https://howtodowithsharepoint.files.wordpress.com/2015/06/16.png?w=450&h=345
Since this demo needs to have another site also, so we got one more site created called “Child Site 0001”, which is a child site to “Development” Site as shown below:
https://howtodowithsharepoint.files.wordpress.com/2015/06/21.png?w=450&h=224
Next thing is to launch SharePoint 2013 Designer, and connect to “Development” Site and browse Workflow section as shown below:
https://howtodowithsharepoint.files.wordpress.com/2015/06/31.png?w=450&h=182
Now in order to showcase how to copy a Workflow from one site to another, we need to create a workflow (SharePoint 2013 Platform) no matter if it is Reusable or List Workflow.
In this demo we would be creating a Reusable Workflow as shown below:
https://howtodowithsharepoint.files.wordpress.com/2015/06/41.png?w=450&h=354
Keeping this workflow functionality as simple as possible, we will add only one activity “Log to History List” and set the message to be displayed in the History Log, Check for Errors, Save Workflow Definition and lastly Publish Workflow Definition as shown below:
https://howtodowithsharepoint.files.wordpress.com/2015/06/51.png?w=450&h=320
As soon as we are done with the steps above we can see a new Workflow added to the Reusable Workflow Section as shown below:
https://howtodowithsharepoint.files.wordpress.com/2015/06/61.png?w=450&h=132
Now let’s add this workflow to a list and see it in action.
Visit any list and from the Ribbon choose Workflow Settings => Add a workflow as shown below:
https://howtodowithsharepoint.files.wordpress.com/2015/06/71.png?w=450&h=141
Select a workflow that we have just published.
https://howtodowithsharepoint.files.wordpress.com/2015/06/81.png?w=450&h=183
Configure the workflow association as shown below:
https://howtodowithsharepoint.files.wordpress.com/2015/06/91.png?w=450&h=279
Now Start the workflow on any list item as shown below:
https://howtodowithsharepoint.files.wordpress.com/2015/06/101.png?w=450&h=194
https://howtodowithsharepoint.files.wordpress.com/2015/06/111.png?w=450&h=132
https://howtodowithsharepoint.files.wordpress.com/2015/06/121.png?w=450&h=212
https://howtodowithsharepoint.files.wordpress.com/2015/06/131.png?w=450&h=188
And that’s it. We are all done with our preparations on Parent Site.
Now it is time to move on to Child Site and see what it is up to.
So if we investigate the Child Site through SharePoint Designer we found currently no workflow is listed under Reusable Workflow Section.
https://howtodowithsharepoint.files.wordpress.com/2015/06/141.png?w=450&h=164
Also if we verify the list of available Associations on any list, we won’t find any such Workflow listed under as shown below:
https://howtodowithsharepoint.files.wordpress.com/2015/06/151.png?w=450&h=222
In order to trigger workflow copy action we got a super simple User Interface ready with a single button “Copy Workflow to Child Site” which is hooked up with an event handler that will execute the code to Copy the Workflow from Parent to Child Site.
https://howtodowithsharepoint.files.wordpress.com/2015/06/161.png?w=450&h=143
Now as I promised in one of my earlier Blogs on Workflow services, we would need the Key information on workflow in Parent Site to get it copied to Child Site.
In case you like to know exactly how can we get this key information about a workflow please visit my blog SharePoint 2013 : Introduction to Workflow Services API
https://howtodowithsharepoint.files.wordpress.com/2015/06/17.png?w=450&h=167
Now let’s do the code dissection to know the complete driving logic as referred in the code below:
https://howtodowithsharepoint.files.wordpress.com/2015/06/18.png?w=450&h=96
1.Specify the Workflow GUID which you want to Copy from one site to another
2.Get the Current Web and Collection of Webs inside it as we need to copy Workflow to a child site of the current site.
3.Create an Instance of WorkflowServiceManager to start the interaction with Workflow Manager Component with reference to the Parent Site
4.Get the Workflow Definition based on the Workflow GUID specified in step 1
https://howtodowithsharepoint.files.wordpress.com/2015/06/19.png?w=450&h=75
https://howtodowithsharepoint.files.wordpress.com/2015/06/20.png?w=450&h=38
5.Set the Workflow Display Name in the Child Site, this is a mandatory step which should not be ignored. The Key take away here is that we need to set the Display Name of this workflow same as it was in Parent Site, else it would not get saved.
6.Get the reference of the Child Site
7.Call the SaveWorkflowDefinition custom function by passing reference to the Child Site.
8.In SaveWorkflowDefinition custom function create an instance of WorkflowServiceManager to start the interaction with Workflow Manager Component but this time using the reference of Child Web
9.Save Workflow Definition
10.Publish Workflow Definition to Child Site
Once we are done with all the steps we can surely see a new Workflow that has been added to the Reusable Workflow section on the Child Site.
https://howtodowithsharepoint.files.wordpress.com/2015/06/211.png?w=450&h=167
If we launch the child site in browser and visit any list to see the available Workflow Associations which are available to the List, we will get the new workflow (which is just copied from Parent Site) listed as shown below:
https://howtodowithsharepoint.files.wordpress.com/2015/06/22.png?w=450&h=120
https://howtodowithsharepoint.files.wordpress.com/2015/06/23.png?w=450&h=165
https://howtodowithsharepoint.files.wordpress.com/2015/06/24.png?w=450&h=284
Now in order to test this workflow, add it to the list and configure the association as needed.
https://howtodowithsharepoint.files.wordpress.com/2015/06/25.png?w=450&h=395
Now Start the workflow on any list item in the list.
https://howtodowithsharepoint.files.wordpress.com/2015/06/26.png?w=450&h=243
https://howtodowithsharepoint.files.wordpress.com/2015/06/27.png?w=450&h=135
And if we can see the same output as Parent Site then this will prove that the Workflow Definition is copied correctly from the Parent Site.
https://howtodowithsharepoint.files.wordpress.com/2015/06/28.png?w=450&h=200
Though this is quite a simple approach but can be leveraged to implement & automate more complex business solutions.
Hope you find it helpful.