Thursday, July 4, 2013

Generate word document from list data from SharePoint Designer workflow



In this post I am going to show you how to generate word document in document library from SharePoint list item. 

Let's first see what all we will need.

We will first take list with metadata. We also need to have the same metadata with the same field type also in the document library. The number of fields do not matter but what matters is the type of field.

We will create a list item, we will have one workflow which will trigger and then create a document based on the metadata which we filled in list item. Document will have the content from the list item. 

We also need to create a document template which has fields from the document library. so when the workflow will trigger, it will set the metadata and based on it document fields gets replaced inside document.

I have one list called orders and here are the fields.



We need to create same fields with same type as site columns so that we can add them to the document content type.

Here are the site columns.



Now we have created custom document content type called orderdocument.



Now we will add previously created site columns to this content type.



So now let's create document library called order documents and add the order document content type that we've created.



Before we move on to creating workflow we need to update our document template with the fields. so go to the library settings. Click on order document content type and you will see the content type screen.



Now click on advance settings. 



Click on edit template.

It will open up the blank word template.

Add these text to the document. 
 


Now click on the insert tab from ribbon and find insert document property.



All we have to do now is add respective property against respective label. In real business scenario you may have proper template and then we fill in values in that template.



Save the template.

We are now set writing a workflow on order list. Open up SharePoint designer and connect to the site. 

Click on list workflow and click on orders.


 
Give name to the workflow.

From actions select create a list item.



Click on this list and then select order document.

You will get this window



But you will see that the default content type is selected as document. Select the content type and click on modify and select order document content type.



Now click on add. Select Order Description and then set it to the order description of the list item. Repeat the same for all remaining list items.



you should have something like this.



Now publish the workflow.

Go ahead and add a new item now in orders list. As our workflow is attached to the orders list, it should execute and complete.

See below list item with workflow status as completed.



Now go to the Order documents library.



Click on the document to open it.



and there you go. Sometimes template needs to be in .docx format instead of .dotx. Not sure what is the reason. So try that yourself.

5 comments:

Masoud said...

Such a great post! thanks a ton :)

hafin2 said...

Nice post! But i get an error when i open the final document in the Order library. The error says it cannot open because there is some problems with the content?

Do you have a suggestion to fix this?

hafin2 said...

Nice post! But i get an error when i open the final document in the Order library. The error says it cannot open because there is some problems with the content?

Do you have a suggestion to fix this?

Anonymous said...

Thank you for this post. However, when I open the final doucment it is completely empty. I also do not see the default text I have added

Chris K said...

Hafin2 - I got this error before changing the template file from dotx to docx.




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com