Wednesday 7 August 2013

SharePoint 2013 Workflow to set Title field automatically

There are times where you need SharePoint to set some properties of the file/item automatically for you. For some scenarios you could use metadata with default values for file/items columns. For other scenarios you need a workflow to populate the item's columns e.g Title.

It could be a variety of reasons why this might be the case.
Some of the reasons could be:

  1. to overcome the limitation of SharePoint losing file/item's built-in properties 'Created', 'Created at', 'Created By' or other metadata on file/item move to another repository
  2. to be able to take advantage of functionality that does not allow use of built-in properties or GUID e.g. Labels in Information Management Policy
In our scenario, users expressed that they want some naming convention for their files. They also mentioned (having worked on a File Server for years) that sometimes they do not know where the file belongs to, who has originated it and how old is the file. Well we cannot rename file names of the files users create/upload into SharePoint. Files can come from the variety of sources. They can be downloaded from other applications with meaningful names already there. We do not want the frustration of users shouting at us saying 'I cannot name the file as I want'.
For this instance, we want something that will happen in the background (on top of the user specifying file's metadata)

To create a 'permanent sticker' that will stay always as an attribute of the file in the Title field, we need to develop a workflow, that will automatically set the item/document's title based on the attributes we want. We do not want the workflow to run on an item/file change since we want to capture its attributes only on creation. This specification can be defined on the Workflow Settings page in SharePoint Designer.

Currently, BoostSolutions offer a similar solution and charge $500 for it. I have developed the workflow myself and it works flawlessly.

If you do not have access to Visual Studio and/or your programming skills aren't very good, you can use SharePoint Designer to achieve what you want. Using SharePoint 2010 Workflow Platform, I developed something like this:


Starting: First, you starting your workflow and logging that the process has actually started (good practice for troubleshooting).

Set global Variables: For our Auto Populated title, we want build-in attributes that can be lost on file migration to be included. Therefore we set up global variables MyCreated to copy Created data and MyCreatedBy to copy Created By attribute. We respectively log this to the workflow history so we know if the steps have actually took place.

Processing: The file created may not have a Title in it (default behaviour). We then want to set the title field to the string: 'Created on Variable:MyCreated by Variable:CreatedBy in specific location'

If the Title is already there (e.g. the file uploaded to SharePoint has a Title already or the user, while creating a file types the title in Document Information Panel), we want this Title to be copied to the workflow's variable and changed to the workflow's variable containing the string we want to appear in the Title field (MyTitle2 in this case).
Similarly, we have to log every action so we know what is happening whilst the workflow is proceeding.

Complete: Then we complete the workflow and log that in the workflow's history.

If for some reason the workflow does not complete, is stalled, stuck at 'Starting', 'Processing' or whatever, click on the link saying 'Starting', 'Processing' etc. and you will be taken to the workflow history.
You will see the respective steps of the workflow as they happen one after the other (since we are logging every step). If you see, that the workflow is not progressing to the next step required in the sequence, then troubleshoot that step in the workflow. After you've done this (or on every update to the workflow), make sure you update and publish the workflow so the changes are pushed down to every list/library associated with this workflow.

In my case, you can see the workflow history logging every step as they should happen:

New File - No Title (default behaviour):


 New File with Title in it or specified by the User on creation:



I tested it with another user too and it worked flawlessly.
Now you can use this workflow as a template or as a global workflow which you can download for every site and use for every list/library you want (amending the string in a Title field to hold info relevant to the location of the file).

Stay tuned for more SharePoint 2013 in practice!

UPDATE:

You will have to update your workflow in order for it to work on 'Check Out-Check In' enabled lists and libraries.
You just need to check out and check in the file in your workflow process. My changes to check out and check in items will be universal as I will modify the workflow commands to apply them to the 'Current Item', so I can use it globally, as opposed to the list the workflow is running on. Here are the steps required to take: [re-blogged from Kim Frehe post available here]:

#################################################################################

A modification to the .xoml file that tells the workflow to use “this item” regardless of the list.
Disclaimer: If you want to model an out of the box workflow, always copy and modify…it is never a good idea to modify the out of the box global workflows directly.
Steps:
1.    Set up your workflow with placeholders for your check-in and check-out actions.
2.    Add “Check out item in this list” and “Check in item in this list with Comment” actions to the appropriate steps on your workflow.
a.     Do not select a List. Leave the “this list” as is.
3.    Save your workflow.
4.    Go “All Files”, “Workflows”, click on the workflow name in the window below “All files” so that your workflow displays it’s files in the window on the right side of the screen.
      5. Right click on the .xoml file and click “Open With”, “SharePoint Designer (open as XML)”
      6. Click Ctrl+F to find “CheckOutItemActivity”
7.    In that row you will see: ListItem={x:Null}” ListID=”{x:Null}”
8.    Replace “x:Null” in “ListItem=” with “ActivityBind ROOT, Path=__item”
9.    Replace “x:Null” in “ListId=” with ActivityBindROOT, Path=__list”
10.  Click “Ctrl+F” to find “CheckInItemActivity”
11.  Repeat steps 8 and 9 for this line item. 
12.  Save this file.
13.  Click on “Workflows” and click on the workflow name to open the workflow file as you normally would.
14.  Do not make any changes but click “Save”
15.  When Designer pops up to say that a different version of the .xoml file was saved, and politely asks if you want to replace that file with this one click “No”.
16.  Close Designer and re-open it
17.  Click on “Workflows” and click on your workflow to open and edit it.
18.  Find the step where you checked out and checked in the item.

The action should say “Check out item in Current Item” and “Check in item in Current Item with comment: xxx”


Copyright Kim Frehe
#################################################################

When you configured your .xoml file correctly you should see your 'Check Out' and 'Check In' placeholders updated to 'Current Item':