Monday, June 30, 2014

Creating a SharePoint Workflow

I wrote two previous blog posts about creating workflows with SharePoint Designer:

What are SharePoint Workflows

and

SharePoint Workflows – first steps

This post follows on from those.

In this case we’ll create a simple workflow that sends an email when a document is added to the library.

clip_image002

Firstly give the workflow a name and a description and then select the platform type (which you normally leave set as SharePoint 2013 Workflow) and press the OK button to create the workflow.

SharePoint 2013 allows you to create either SharePoint 2010 or 2013 Workflows. Each has differences and the following:

What’s new in workflow in SharePoint 2013

Is the recommended place to start when it comes to understanding the differences between the versions. In most cases you will want to work with SharePoint 2013 Workflows.

clip_image004

This now takes you to a location where you can edit and create the workflow.

For a SharePoint 2013 Workflow you will see a stage box or “block” divided up into two sections. The upper box is for a set of Actions and the lower box (Transition to stage) which allows you to jump to other stages you may have created. This Transition to stage is a unique feature of SharePoint 2013 workflows and allows you to bypass code by ‘jumping’ to certain locations.

You should also see a blinking cursor under the top section as shown above. You position this cursor where you wish to insert a command in the workflow.

clip_image007

In the Ribbon Bar you should see a number of buttons contained in the Insert section as shown above. Press the Action button to insert an execute command.

clip_image010

When you press the Action button you will see a list of all the actions that can be taken, as shown above. Scroll through the list until you locate Send an Email and select it.

clip_image013

You should now see that command displayed in the workflow area below in the location where your cursor was as you can see above. The cursor now moves to the line just below what you inserted.

You will also note that the text these users is hyperlinked. This indicates that additional details are required for this command to complete. In this case you will need to specify the email address that you wish the email to be sent to. To do that simply press the hyperlinked text these users.

clip_image015

Now the additional settings for each Action will vary. In this case you should now see a dialog as shown above, basically asking you to complete the details for the email.

clip_image017

There are plenty of options that can be configured here (including data lookup) but we’ll keep this simple and plug in just an email address, subject and some text in the body.

When complete, press the OK button to save.

clip_image020

You should now see that the workflow action has updated with the email address you entered. You can modify these properties again at any time by simply clicking on the hyperlinked email address.

clip_image023

Although this is the only command that will be run by the workflow at this stage it is good practice to correctly terminate the workflow. To do this, click once in the Transition to stage area so the cursor appears there as shown above.

clip_image026

Select the Action button again from the Ribbon Menu. From the menu that appears select Go to a stage.

clip_image029

Once again, this will place the Action in the workflow window as shown above.

To complete the options for the Action select the hyperlinked text a stage.

clip_image032

From the drop down menu that appears select End of Workflow. Doing so will ensure that the workflow does not continue to run in the background consuming resources.

clip_image034

The completed workflow should now look like the above.

clip_image036

You now need to set the workflow so it runs whenever something new is added to the Document Library. To do this locate the Workflow Settings button in the Ribbon Menu in the Manage section and select it.

clip_image039

On the right hand side you will see a box called Start Options as shown above. Place a check in the Start workflow automatically when an item is created.

clip_image041

If you need to return to your workflow and edit it select the Edit Workflow button under the Edit section on the Ribbon Menu.

You can also Save your workflow if need be but in this case we are ready to Publish to the SharePoint site so we select the Publish button from the Ribbon Menu.

clip_image043

You should now see the workflow being Published to the SharePoint site. If there are any errors you will be notified and have to make changes.

If there are not errors, your workflow has been successfully Published to SharePoint.

You can check that your SharePoint element has a workflow attached to it by navigating to that location (in this case Documents).

clip_image046

If you then select the Library tab at the top of the page.

clip_image048

Then the Workflow Settings button on the very right of this.

clip_image051

You should then see the name of the Workflow you just created.

clip_image055

If you look closely at the SharePoint element that now has a workflow attached you will see an additional column has been added to the default view. This column is the same as the name of your workflow (in this case Send email workflow as shown above).

clip_image034[1]

When you trigger a workflow (in this case by uploading a new document) the new workflow column will report what stage the workflow is at. Remember, that each “block” of Actions and Transition to stage is designated by its name.

In this example, there is only one code “block” called Stage 1 as shown above. Thus, when the workflow is triggered by the addition of a new document to the element the workflow executes and start and ends on Stage 1. This status is then reported in the workflow column in the SharePoint element. This allows you to more easily debug your code since it allows you to easily see where a workflow is currently situated.

clip_image057

As you can see from the above, the workflow was completed and an email has been sent with the subject and text created in the workflow.

You can now return and edit the workflow and create additional Actions. You can create additional workflows for the same elements if you wish or you can create them for other elements.

Conclusions

SharePoint workflows provide the ability to automate processed in SharePoint. However, workflows are not like pure coding, they are more like constructing a flow chart. They are great for simple and straight forward processes but become quite cumbersome and difficult to create when the task gets complex or involved.

Best practice is always to map out your workflow prior to developing with SharePoint Designer. Keep your code as simple as possible and make sure you maintain good documentation elsewhere about what you have created because SharePoint Designer doesn’t provide for adding comments to your code.

If you ever need to debug a workflow, break it down and look at the stages that are reported in the SharePoint element to which the workflow is attached. Also, don’t overlook the workflow start conditions. If you set a workflow to commence when something changes, that means when ANYTHING changes. That may mean you have multiple workflows running at the same time causing confusion. That is why it is so important that you programmatically terminate your workflow upon completion, rather than simply letting the process end.

Use correctly, SharePoint Designer and workflow can be huge source of productivity improvements. Use incorrectly or poorly implemented they can be a huge source of frustration. Remember, SharePoint Designer is not the only tool that you can use to create workflows. You can use Visual Studio for far greater control, but for that there is a much greater investment in both time, material and knowledge. SharePoint Designer allows a balance to be struck and be used by people who aren’t software developers.