Posted by: R Manimaran | December 12, 2014

SharePoint 2013: Using HandleBars.js in SharePoint –Part 3

In Part1, we have seen how to create a Handlebars template and use it SharePoint 2013 REST API output.

In Part2, we have seen how to format a SharePoint date time value using Momemt.js

In this article let us see if we have a Lookup Field column in the list.

I have created a new SharePoint List named “Domain”. In the “Employees” List I have added the title column of the Domain list as a Lookup Field. Below is the view of the Employees list after adding my Lookup column.

EmployeesDomainList

For the Lookup column the REST API JSON output will be different.

REST API URL Endpoint:

/_api/web/lists/getbyTitle(‘Employees’)/items?$select=Title,Created,Modified,Domain/Id,Domain/Title&$expand=Domain/Id,Domain/Title

The JSON output for the above will be like below. Inside the Domain node we are having the title value of the Lookup field.

JSON output

We need to include the above Lookup field in the HandleBars template. So our updated template will be as below.


<!-- Handlebar Js Template Starts Here -->
<script id="employees-template" type="text/x-handlebars-template">
<table>
<thead>
<th>Employee Name</th>
<th>Designation</th>
<th>Location</th>
<th>Expert</th>
<th>Domain</th>
<th>Created</th>
<th>Modified</th>
</thead>
<tbody>
{{#results}}
<tr>
<td>{{Title}}</td>
<td>{{EmpDesignation}}</td>
<td>{{Location}}</td>
<td>{{SME}}</td>
{{#Domain}}
<td>{{Title}}</td>
{{/Domain}}
<td>{{FormatDate Created}}</td>
<td>{{FormatDate Modified}}</td>
</tr>
{{/results}}
</tbody>
</table>
</script>

Helper Function for format date


<script>
Handlebars.registerHelper("FormatDate", function (date) {
var format = 'M/DD/YYYY';
return moment(date.toString()).format('LL');
});
</script>

On running this script we will have the below output in the page.

DomainOutput

Happy coding..

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: