Document Generator

Discover how to use the Document Generator premium feature to help you create professional documents from your SharePoint list items.

 

In this video, we explore how to use the Lightning Forms Document Generator to create Microsoft Word documents from a SharePoint list item. The Word document can then be printed, shared, or saved as a PDF. 

 

 

Before you start using the Lightning Tools Document Generator, you first need to create a Microsoft Word document that references the column names within your SharePoint List and/or sublists. Once this has been completed, you can upload the Microsoft Word document using the Document Generator custom action.

*Note that the Document Generator is a premium action with Lightning Forms. 
 


Creating your Document Template 
 

  1. Create a new Microsoft Word Document.
  2. Style the document to resemble the look and feel of your organization’s requirements.
  3. Reference the fields on your SharePoint list form using square brackets around the field name. e.g. [[Title]]. (Remember to use the programmatic name and not the display name. e.g. ‘Approved By’ may have a programmatic name such as ‘Approved_x0020_By’ as shown below. 

Referencing the SharePoint Field names within your Microsoft Word document. 
 
  1. Open the Expression Builder within Lightning Forms against the Display Form, Edit Form, or New Form to see a list of fields with their display names and programmatic names, as shown below: 

Using the Expression Builder to determine the field names within the SharePoint Form. 
 
  1. See the Syntax section below for how to add things like images and hyperlinks.
  2. Save your Microsoft Word template and name it appropriately e.g. Expense_Report_Template.docx 

     

Referencing Fields in a Sublist


If you use a sublist within your SharePoint list, you can display the column names within a loop statement, allowing each item within the sublist to be displayed in the Word document. To create the Table and Loop statement:

  1. Create a Table with two rows, and the number of columns based on how many fields you wish to display from your sublist within your Microsoft Word document. One row will be for the header, and the second row for the field references. If you wish to display First Name, Last Name, Email, Phone from a sublist, you will need a 4 x 2 table. 

Create a Microsoft Word Table in your Word Document 
 
  1. Provide column headers for each field and format your table to your requirements.
  2. In the first column, second row, add a Loop referencing the sublist name and the column name you wish to display using the format: [[loop sublist name]][[Title]]. An example is provided below for a sublist named Expense Items. 

Referencing fields in a sublist
  1. Add the other required columns e.g. [[ClaimAmount]].
  2. Add the final column and close the loop using [[end loop]]
  3. Save your document.

Download a sample Word Document

ExpenseReportPrint.docx (215 downloads)



Create a Custom Action on the Display Form or Edit Form within your SharePoint List. 
 

It is recommended to use the Document Generator for a saved list item. Therefore if you are using a New Form to create New Items, the first action would need to be ‘Save Form’. If you are using the Edit Form or Display Form, you don’t need to use a Save Form action before the Generate Document action.

 

  1. Edit the SharePoint form using Lightning Forms. 
     
  2. Choose ActionsCommand Bar to add a new action button to the top of your form.
  3. Click the + icon in the Command Bar Actions side panel. 

Adding a custom command bar action 
 
  1. Provide a display name and set an icon for your custom command bar button.
  2. Click Configure Actions. 
     

 
 

  1. Select Generate Document from the list of Action Types
  2. Upload your saved Word Document
  3. Select For single item from the Data field.
  4. Select your SharePoint List (Just the main list, not the sublist)
  5. Set the Item ID field to [[ID]]
  6. Leave the default Target to Open in Browser
  7. Set the filename pattern for your reports. e.g. ‘ExpenseReport_[[Title]]_[[ID]]’
  8. Click Save 

Configuring the Document Generator


You can now test your Command bar action by opening or editing an item within your list. 
 


Create a Custom List Action Within a SharePoint View


If you use Lightning List Actions, you can also create Command Bar buttons within a SharePoint List View. 
 

  1. Click Lightning List Actions within your SharePoint List.
  2. Choose a name, icon, and set the action visibility to ‘Selected Items’.

  1. Choose a name, icon, and set the action visibility to ‘Selected Items’.
  2. Configure the list action using the steps 6-11 from above.
  3. The Action button will display when you select multiple list items within your view. 
     


Document Generator Word document syntax 
 

Template expressions Any kind of string Template Expression [[Title]] [[Firstname]]
Assignment Expressions Start with "[[=" and any kind of string Expression [[= [[Title]] + ", " + [[Firstname]] ]]
Function Code Expressions Start with "[[{" in the Tag property. Write your complex expression as content in the placeholder [[{ var arr = [[@Loop('YourField', [[{ return [[Total]]; }]] )]]; var sum = 0; arr.forEach(n=>sum +=n); return sum; }]]
Lookup Values Use the [[Lookup.FieldInternalName]] notation to get any value from the target list of a lookup Examples:  
[[Department.Code]]  
[[Department.Author]]  
[[Department.Key]] -> to get the item ID of the department
If Conditions Start with [[if …]] and any kind of boolean Expression (Condition).  
Ends with [[end if]]   
Everything between the starting and the ending tag will be visible if the condition is true. This can be text, an image etc.
[[if [[Budget]] > [[Outgoings]] ]]  
[[Title]]  
[[end if]]  

[[if {return ([[Budget]] > [[Outgoings]]);} ]]  
[[Title]]  
[[end if]]  

[[if [[Status]]!="Completed" ]]  
[[Title]]  
[[end if]]
Loops Start with [[loop]] for looping over multiple items of the selected list.  
If a CAML Query is configured for this action link, looping will be filtered according to it.  
Start with [[loop SublistTitle]] for looping over the sublist items filtered by lookup to the parent list. Use the filterlookupfield('field_internal_name') param to specify which lookup field to use for sublist in a case when several lookups on the same list exist. It’s optional where there is just one lookup field. Also RowLimit is hardcoded for 200 items for query, but the required number can be specified with the optional rowlimit(number) param.  
If list that is not a sublist is selected (doesn’t have lookup to the list selected in action link configuration), no filtering will be applied.  
Iteration ends with [[end loop]]
[[loop]]  
[[Title]]  
[[end loop]]  

[[loop SubListTitle filterlookupfield('field_internal_name') rowlimit(number)]]  
[[Title]]  
[[Firstname]]  
[[end loop]]
Filtered Loops Where SomeView is a view .aspx page title, such as AllItems.  
The order and filter of the view are applied. If CAML query is added (optional), it is united with the view query using an ‘and’ operator.
[[loop SublistTitle view('SomeView') query('<Where><BeginsWith><FieldRef Name="Title" /><Value Type="Text">[[{return [[Title]];}]]</Value></BeginsWith></Where>')]]  
[[Title]]  
[[end loop]]
Loops over documents in sublibraries Use the Scope="Recursive" property on the query to get documents in a folder, when used as sublibrary of a form. Combine recursive scope and filter in CAML query to filter the listed document’s metadata. [[loop SublibraryName query('<View Scope="Recursive"></View>')]]  
[[FileLeafRef]]  
[[Title]]  
[[OtherField]]  
[[end loop]]  

[[loop SublibraryName query('<View Scope="Recursive"><Query><Where><Eq><FieldRef Name="IsActive" /><Value Type="Boolean">1</Value></Eq></Where><OrderBy></OrderBy></Query></View>')]]  
[[FileLeafRef]]  
[[Title]]  
[[OtherField]]  
[[end loop]]
Nested Loops Looping over a sublist inside of the loop over multiple items of the parent list is supported.  
Looping over several sublists, or a sublist of a sublist are supported as well.
It is possible to loop over table rows by adding the loop inside the table. In the case of nested loops, if loop on parent list is placed into the external table, this will render separate table for each parent item
Loops over table rows It is possible to loop over table rows by adding the loop inside the table. In the case of nested loops, if the loop on the parent list is placed into the external table, this will render a separate table for each parent item [[loop  
SubListTitle]]  
[[Title]] [[Firstname]] [[Lastname]]  
[[end loop]]   

[[loop]] [[Title]]  
[[Title]] [[Firstname]] [[Lastname]]  
[[end loop]]   
[[end loop]]
Image Show an image inside the document.  
You can optionally specify the static width and height of the image and also a max/min width and height.  
Width/height parameters are not mandatory; they can be '' (empty string) or null. 
[[image ([[Picture]], width, height, maxwidth, maxheight, minwidth, minheight)]]   

Without width and height: [[image ([[Picture]])]]   

Static width and height: [[image ([[Picture]], 50, 50)]]   

Static width: [[image ([[Picture]], 20)]]   

Static height: [[image ([[Picture]],,150)]] [[image ([[Picture]], '', '', 100, 300)]]  

[[image ([[Picture]], maxwidth=100, maxheight=300)]]  

[[image (url=[[Picture]], maxwidth=100, maxheight=300)]]
Link Create a link in the document [[link ([[Hyperlink]])]]  

[[link ([[Hyperlink.Url]], [[Hyperlink.Description]])]]  

[[link (url=[[Hyperlink.Url]], text='linktext')]]

Was this article helpful?

Can’t find what you’re looking for?

Our world-class Customer Success team is here for you.

Contact Support