Sunday, December 30, 2012

A Managed Document Center – Part III

This is the third of a series of posts about creating a Managed Document Center in SharePoint Online. The purpose is to create a document center that is able to route and rename uploaded documents to their respective folders based on metadata associated with them. To see a full description of the resultant product, click here.

As mentioned on the previous posts, I have divided the job in the following steps:
  1. Create the necessary Term Sets in the Term Store. 
  2. Create a new site using the Document Center template. 
  3. Enable and set up the Content Organizer. 
  4. Create a site Content Type using Document as Parent Content Type. 
  5. Edit the default Documents library with the columns and views needed. 
  6. Create a workflow to rename the documents. 
  7. Save the Documents library as template and create a new document library for each facility. 
  8. Create Content Organizing rules to route the documents to his final location. 
  9. Redirect the default “Upload a Document” button to the Drop Off Library.
To see step 1 click here. To see steps 2 to 5, click here. In this post I will cover step 6.

5. Create a workflow to rename the documents

The managed metadata columns in SharePoint have some limitation as listed by Michael Pisarek in this post. One of these limitations is that we cannot use them in formulas and they also need to be trimmed in order to get the correct string to be used as the name of the document.

My first initial though was to develop custom code in order to get this operation to work, but custom code is always our “last resource”, because our idea on each solution we provide is to be easily replicable. So I came out with a solution using a workflow.

We are going to need SharePoint Designer to create this workflow. Click on Site Settings > Edit in SharePoint Designer in order to open SharePoint Designer. If you don’t have it already installed, you’ll be prompted to download it and install it.

Once in the SharePoint Designer main page, click on List and Libraries on Site Objects panel the left, and the open Documents on the right panel.

On the Documents page, under the Workflows section, click on New...

Name the workflow as Rename Documents. A new workflow in edit mode will be showed with an empty Step 1. Click on it and click on Action in the ribbon, select Update List Item. Click on this list field and in the Update List Item dialog, leave List as Current Item. Click in the Add button and select the values as the following image:

Click OK twice to close the open dialogs and click the Save button. Next, on the navigation bar at the top of the workflow editor, click on Rename Documents to see the properties of the workflow, then, under Start Options check the options to start automatically the workflow when an item is created and when an item is changed.

 Click on the Save and then the Publish button in the ribbon.

Now, let’s go back to our Documents library in SharePoint and let’s upload a document. If you take a look to the Rename Documents column in the library right after uploading the document, you’ll see that it says that an error has occurred. If you click on the error, you will find that the information provided by SharePoint is not clear. So, as a matter of test, let’s go back to SharePoint Designer and edit the workflow to update the Title instead of the Path and Name to see what the outcome would be. Save and publish the workflow and upload a new document.

The workflow now has completed successfully  but if we check its properties, we will see that the title has the product name, the pipe character and the GUID of the managed metadata tag. The pipe character is not allowed as a file name, that’s why the workflow errors out. We also don’t want to include that GUID as part of the document name.

We need to trim the product and the document type columns, but unfortunately SharePoint Designed doesn't include support for common string functions. We could do it directly in SharePoint using a calculated column, but calculated columns don’t work with managed metadata. The approach we will use is to create a set of temporary columns where to put that data and work around these limitations.

Back in SharePoint, go to the Library Settings of the Documents library. Under Columns, create two single text columns called tempProduct_raw and tempDocumentType_raw with any string as default value. Don’t add them to the content types and or the default view.

These two columns will be used by the workflow to store the raw string composed by the product name or document type plus the pipe character plus the GUID.

Now, create a new calculated column called tempProduct_name with the following text as formula:
Don’t add it to the content types or default view.

Create another calculated column called tempDocumentType_name similar to the previous one with the following formula:

These two new calculated columns will remove the pipe character and the GUID from the product and document type respectively.

Let's go back to SharePoint Designer and edit our workflow. The first step will update the tempProduct_raw and tempDocumentType_raw columns with the values of the product and document type columns respectively. You may need to refresh SharePoint Designer in order to see the recently created columns.

Now, add a new step after Step 1 and select the Pause for Duration action in the ribbon. Edit it to pause for 0.5 minutes. This step is necessary to give SharePoint enough time to calculate the value of the calculated columns.

Next add a third step after Step 2 and select Update List Item from the action in the ribbon. Edit the Path and Name field with the following string:

The final workflow will look similar to this:

Save it and publish it. Back in SharePoint, upload a new document to test it. Right after uploading the document, the Rename Documents column will indicate that the workflow is still in progress, wait a few seconds and refresh the screen to see that the name of the document has actually changed.

I hope you enjoyed this post!

No comments:

Post a Comment