Creating Word documents containing List fields
I haven’t seen much information on the web regarding how to automatically create Word documents in SharePoint and how to pass list item fields directly into a document.
It’s fairly straightforward to get this working (certainly no code is required, just SharePoint designer) and the technique could be used to automate all manner of business processes. For example, you could create an ASPX page containing drop down fields and calculated fields that automatically creates a quotation document or you could have a form that captures all the details for a job vacancy that would create a standard looking job advert.
In this exercise I will keep things simple and create an application that creates a certificate with an exam name, user name and pass mark passed into the Word documents from a list item. This would make a nice addition to the Quiz Web Application exam. This example requires Word 2007.
Here is an example of the created document with fields (the name, the exam title and mark – shown below in bold, being variables stored in a SharePoint list.
The example has the following components:
· A document library which has a Word document associated which is used as the basis for all new documents
· A list containing the fields that we want to pass into a new document
· A workflow which creates a new Word document and copies the fields from the list into the document
Creating the document library
A document library is really just a special type of list. There are some differences in functionality but many of the features remain the same.
First we need to create a new document library, in your browser:
· (Top right) Site Actions > Create
· Select “Document Library”
· Name: Certificates
· Ensure the “Document template” is set to “Word document”
Next we want to add some columns to our document library for our variables to be added to
1. Click on the “Settings” button in the list view
2. Click on “Document library settings”
3. Click on “Create Column
Add the following columns:
Single line of text
By default, the Title column has already been created in the list so we will use that to store our Exam title field. There are some other columns in that are created for a document library that we will not need to use.
Creating a Word document to use as a template
Next we want to create our word document which will act as a template for all newly created documents in this library. I am a little reticent to use the word template as we will actually just create a normal document (DOT / DOTX) rather than a Word template (DOT / DOTX).
In the settings screen of the document library:
1. Click “Advanced Settings”
2. Click “(Edit Template”)
Word will open the file template.doc which is located in the forms library of your document library.
3. Save the file as a “Word document” (click okay if you are asked to confirm)
This saves the template document from Word 97-2003 format (compatibility mode) to Word 2007 format which is a DOTX file. This allows use the document property field.
We need to associate our document library with the new file, to do this:
1. Document library > List settings > Advanced settings
2. Change the template name to “template.dotx”
Now we are ready to create the template for our certificate, go back into the Document Library > List Settings > Advanced settings and click on the Edit Template link.
You can create a nice looking document leaving space for where our list fields will go. When you are ready to insert our fields, do the following:
1. From the main menu select “Insert”
2. Click “Quick Parts” > “Document Properties”
3. You will see a list of the document properties including the columns we added (Score and YourName)
You can select each of our fields and place them in our document – YourName, Score and Title are the three fields we will insert.
4. Insert each of the fields into your document, they will display like this:
Here is an example of the basic layout:
And here is something a little fancier.
That completes the creation of the document library and the associated word document.
Creating a list to store our fields
We need to create a list to store our values that will be inserted into the document template.
As you can imagine, this is very straightforward:
· Site Actions > Create
· Custom List
· Name: Documents
· Click Create
Then add our two fields into (Title will already exist)
Add the following fields to the Documents list Add the following columns:
Single line of text
You will notice this is exactly the same as what we did for the document library fields. Our final step is to create a workflow that copies the fields from the Documents list into a new document in the Certificates document library.
Writing our workflow to create new documents in the certificates library
We are going to create a simple workflow that does the following
· When a new item is added to the Documents list create a new Item in the Certificates library
· Copy the title, YourName and Score fields from the Documents item into the Certificates item
This will in effect create a new document in the Certificates library that has the fields automatically populated with the values from our Documents list. Here is how:
1. Open SharePoint designer and connect to your site
2. File | New | Workflow
Give the name as “Make a new certificate”
Attach “Documents” list to this workflow.
Select “Automatically start this workflow when a new item is created”
We are saying here, each time a new item is added to the Documents list then run the workflow.
3. Click “Next”
4. Click “Actions” > “Create List Item”
5. Click on the “this list” link
6. On the list selector choose “Certificates”
7. Click on the “Name (for use in forms) ...” entry and click “Modify”
8. In the value box, click the “Fx” button
9. Keep the source as “Current Item” and change the field to “YourName”
10. Click OK, OK
The above steps mean that when a item is added to the list Document then a new document will be created in the Certificates document library and the name of the item will be set to YourName feild.
If more than one item is created with the same name then a unique identifier will be automatically added to the title.
Next we need to add our three fields to the Create List Item action. To do this:
1. Click the “Add” button
2. Set this field: Score
3. To this value: Click the “Fx” button
4. Source: Current Item
5. Field: YourName
Do the same for the Title and YourName values. Your completed List Item dialogue should look like this:
You can now click “OK” and you will see SharePoint designer create the new Workflow and assign it to your list.
We are now all ready to test our new solution!
Putting it all together – testing our solution
Go back to our website and select the “Documents” list and click “New” and complete the fields and click “OK”
Back in the list of items in the Documents list you will notice a new column “Make a new Certificate” has appeared. This column shows the status of the workflow that we created.
Currently it will be showing “In Progress” whilst the workflow kicks and begins to run (creating the new word document in the Certificates library). Wait for a few seconds and then refresh the page (F5). You should see the status has changed to “Completed”
Finally, switch to the Certificates document library. You will see our new document has been created. When you open the document you will see that values that we passed are now contained in the Word document.
Posted: Wednesday, April 15, 2009 2:15 PM by brenclarke