Friday 16 December 2011

Creating Documents with SharePoint Designer 2010 Workflows

SharePoint is great for document management, and building documents is on the agenda of most employees in an organization. Wouldn’t it be great to automate some of that document generation by using simple workflows?

In this article, you will learn how to set up a SharePoint document library and a custom content type to allow for automatic creation of Word documents using SharePoint Designer 2010 workflows.

Before we go on, note that I won’t be explaining every detail of every step. Instead, I’ve recorded a video that shows all the steps for you if you need that. However, you should have some knowledge both about SharePoint and SharePoint Designer workflows if you want maximum benefit from reading this article.

Prerequisites


What we are going to do is create a content type that we will add to a basic document library. The purpose of using a content type is to allow us to override the default document template and to utilize the brilliant new reusable workflows in SharePoint Designer 2010. Finally, we’ll create a workflow in SharePoint Designer 2010 that will create a new document containing data retrieved from a user in the initiation form.

To follow along in the steps here, you’ll want to get SharePoint Foundation 2010 and SharePoint Designer 2010, both freely available from Microsoft. In addition, I’ve just installed Office 2010, and that fact (the just installed bit) is important, because the default security setup will prevent this feature from working, at least as demonstrated here and in the accompanying video.

However, and this is also very important, the method you learn here works just as well in SharePoint 2007. The workflow part will be different because the workflow designer has drastically changed in SharePoint 2010, but the steps and the technique are the same for both versions.

Beyond that, I’ve added a single site collection in which the sole and probably lonely site is a Team Site.

Before we begin, though, you can see this entire technique demonstrated in the YouTube video below.

If the video doesn’t load for you, you can view it on YouTube: http://www.youtube.com/watch?v=T3IXQ2cW5L4.

Creating the Content Type


For the purposes of this article, the content type is of less importance. What matters is how we configure the content type and from which content type it inherits.

First, you want to create a content type that inherits from the Document content type. Name it whatever you like, but I’ve called mine CustomDoc. The important thing is to inherit from the default Document content type.

For this example, you also want to add a unique column to that content type to make it different from the Document content type. So, to your CustomDoc content type, add a new site column, again naming it more or less whatever you want (I’ve used DepartmentName). The important thing here is to use one of the built-in SharePoint Foundation or WSS column or field types. None of the custom field types, including the ones that ship with SharePoint Server or MOSS, will behave as expected.

Next, you want to connect the content type with a document library. In my case, I’ve just used the Shared Documents library, but you probably want to use another library in your production environment. To add a content type to a library, you first need to enable content type management, which you do from the Advanced Settings page of the Library Settings page.

Review the video if you need step-by-step instructions for these tasks


Setting Up the Template


After adding the content type to the library, we next need to modify the template used for the content type. This is where we encounter our first pitfall. Inside the Library Settings page, click the CustomDoc content type, and then click Advanced Settings.

Creating Documents with SPD 2010 Workflows - 01

Because we inherit from the Document content type, we inherit a default template. Unfortunately, that template is blank, and it is also a Word template (.dotx) file type. This means when we open that file by clicking the Edit Template link, Word will create a new document based on the template, not open the template itself.

So, the first thing you need to do is change the template type to be a .docx document instead of a .dotx template. Click to edit the template, save the document as a .docx, and then upload the new document template. Again, refer to the video for detailed steps.

You may need to turn off the protected view in Word to be allowed to open the document. Go to File, Options, and then Trust Center. Click  Trust Center Settings, and go to Protected View to turn off the top two checkboxes.


After you have re-uploaded the document to be used as template, you can now click the Edit Template link to open Word.

Because we are working off the default template, although converted to a document, the document will be completely empty when you open it. You may want to add some text to the document; you can be as creative as you want.

However, what we want to achieve now is to have the document automatically populated with data from SharePoint, and the way to do that is to go to the Insert menu and click the Quick Parts button. This will pop out a submenu from which we will choose Document Property and then the DepartmentName property or whatever you named your column.

This inserts a placeholder, which, when the user adds data, will be replaced with the data the user enters. Now onto the workflow stuff.

Building a Document in a Workflow


Before we begin, there’s one important thing you need to understand, which will make the rest of this walk-through very much easier to understand.

A document is a list item. No really, I’m not joking. In SharePoint, a document is basically a list item like any other list item, such as a task, a contact, or a folder. Yes, folders are list items too.

Before your mind completely blows up from this amazing revelation, let’s consider what this means.

Basically, because a document is a list item, whenever we create a list item, which we can do in a number of ways, we create a new document. Although a full treatise of how this happens is beyond the scope of this article, let’s demonstrate this in practice by creating a very simple workflow.

Fire up SharePoint Designer, and remember this works in SP2007 as well, and connect to your site. Then, create a new workflow. If you are using SharePoint 2010, it may be a good reason to create a site workflow, but you can create a list workflow too. What you shouldn’t to, however, is create the workflow on the document library in which the final documents will end up.

Let’s add a field for the user to fill out as part of starting the workflow, so create an Initiation Form Parameter, which should be a Single Line of Text and be named Department Name or something similar. Now, when users start the workflow, they will be asked to provide a value for Department Name. We will use that value to create our new document.

Next, in the workflow, add a Create List Item action. Click the “this list” link, and in the dialog box, select the document library to which you attached your custom content type. This will give you two values to fill out, one for the Content Type ID and one for the Path and Name, the latter being a required field in the Document content type.

However, we want to create a CustomDoc content type instead, so highlight the Content Type ID value and click Modify. From the drop-down menu, select CustomDoc, as shown here.

Creating Documents with SPD 2010 Workflows - 02

Once you OK out of the Value Assignment dialog box, we want to add the value the user provided to the DepartmentName content type column. Click the Add button, and select the DepartmentName from the “Set this field” drop-down menu before clicking the fx lookup button to set the value.

We asked our user to provide the value for the department name, and that value is now store in a parameter. As such, select the Workflow Variables and Parameters from the Data source drop-down menu, and select Parameter: Department Name from the next drop-down, as shown here.

Creating Documents with SPD 2010 Workflows - 04

Finally, click OK until you get back to the Create New List Item dialog box. You need to add something for Path and Name field, or you may want to repeat the assigning of the department name to that field as well.

Regardless, with these steps done, publish the workflow, and we’re now ready to test it.

Testing


Because our workflow is so simple, it will be quick to test. If you built a site workflow, go to the All Site Content and then to the Site Workflows page.

Click to start your workflow, which will ask you to provide a department name, after which the workflow will complete.

What will happen next is that you get a new item in the document library. Weird, huh? After all, we created only a new list item, right?

Well, open the document, and behold the fruits of your labor. Your document will appear, with the placeholder token replaced with whatever you entered as part of the workflow initiation.

 

I hope you’ve learned some new tricks here that will aid in your usage of SharePoint Designer workflows. Remember to also toast the power of content types, which play a diminished but vital role in this story.

Enjoy!

No comments:

Post a Comment