CodeProject
Though I have been working on SharePoint from the past 4+ years, I didn't notice this awesome feature. We can create / update Word documents dynamically in SharePoint. This is a very useful feature which will be helpful in the creation of standard Word documents like Approval forms / Employee Offer letters, etc. It just takes few minutes to setup.
Let's try to create one sample form (Employee Offer Letter) using this feature.
I'm implementing this example in Sharepoint 2010. We need Design rights on SharePoint site and also Sharepoint Designer, Microsoft Word are needed for this example.
Step 1
Create a Document library to generate and store the Offer letter documents in the site. Create the columns that dynamically need to be updated in the document, e.g.: Name
, salary
, etc. The columns in Library act as properties for a document, which can be added directly in the Word document. In this example, I added 4 such fields
- Employee Name
- Salary
- Designation
- Proposed Joining Date
Step 2
For every Document library, we will have a default file template for that library. This can be a Word document too. To edit it, go to Settings > Advanced Settings. Document template will be available, click on 'Edit Template'. This opens up Microsoft Word with the standard template file (.dotx).
Step 3
Now, writeup the standard content that to be there in the Offer letter and in the places where data is to be filled dynamically, i.e., Employee name, salary, etc. insert the Document Properties that we created in the above steps. To add a document property, Select 'Insert' in word ribbon and Go to Quick Parts > Document Properties. All the above created columns are displayed along with default ones. Select the corresponding property.
Step 4
Once added, click on Save as and change the format from '.dotx' to '.docx' (this step is needed to automate document generation, we can't update a '.dotx' from the back-end.) Now the document looks as in the below screen.
With the above step, we are ready with document library and if we create a new document in this library, it will automatically fill in all the corresponding placeholders and our document is ready.
Step 5
Now, create a document to test it. If you click on 'New Document' in the document library, a Word document with previously created template opens. Also, a form with all the new fields we created will be displayed on top.
Step 6
If we fill-in the corresponding details, the place holders will be automatically updated and once you save it, a new document with these details is created in the document library. A sample document will appear as in the below screen after filling all details.
To create this document dynamically, we need to create a new item through back end in the library, by filling all the properties we created. This can be achieved in multiple ways.
I created a list with the same details and using a workflow on the list, when a new list item is created, it will create a new item in the document library by filling all corresponding properties in library.
To do this, follow the below steps.
Step 7
Create a Sharepoint list with the same columns we created for library.
Step 8
Open Sharepoint designer and go to the list we created and click on New workflow. Fill in the Workflow Name, description.
Step 9
Select action 'Create List Item' from Actions tab in ribbon.
Step 10
Click on 'this list' and choose the document library we created.
Step 11
Using 'Add' option, choose each column and fill it with the current item corresponding column data. Also, modify default field 'Path and Name' with the convention you prefer.
Step 12
Once all the fields are mapped, save the workflow. And, select the option to 'Start workflow automatically when item created' and publish it. I also added another step to send email with the newly created document link.
With these steps, we completed our configuration / development process for the functionality.
To test it, create a new item in the list with all the details. Then, open our document library to see the newly created document with the details.
This feature not only automates document creation, but also helps us in logging the details in online system which will be helpful for future references / audit purposes.