Site Column Display Templates – KPIs

Furthering my theme of Site Column Display Templates, this is really simple but one that I will definitely reuse over and over.

Site Column
Title: kpiIcon
Type: Choice [Red, Yellow, Green]

This site column can now be added to any list or library as usual.

Display Template
You can copy or download the complete display template from GitHub.

var paylord = paylord || {};
paylord.kpi = paylord.kpi || {};

paylord.kpi.viewFieldTemplate = function(ctx) {
var kpi = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];
var suffix = "";
if (kpi == "Green") {
suffix = "0.gif";
} else if (kpi == "Red") {
suffix = "2.gif";
} else {
suffix = "1.gif";
}
var ret = "<img title="&quot; + kpi + &quot;" src="/_layouts/images/kpidefault-&quot; + suffix + &quot;" alt="" />";
return ret;
};

(function () {
var fieldCtx = {};
fieldCtx.Templates = {};
fieldCtx.Templates.Fields = {
"kpiIcon": {
"View": paylord.kpi.viewFieldTemplate,
"DisplayForm": paylord.kpi.viewFieldTemplate
}
};

SPClientTemplates.TemplateManager.RegisterTemplateOverrides(fieldCtx);

})();

Told you it was simple. The scoping object is set up to keep it behaving nicely then my function checks the selected choice value (Red, Yellow or Green) and returns an image using the standard SharePoint icons.

In this case I have not made any change to the edit mode but applied my function to both the View and Display modes.

PowerShell
The ability to add the JSLink property through the UI to a Site Column seems to have been forgotten by Microsoft so I am hoping we can encourage them to make this simple enhancement – add your vote through User Voice.

In the meantime we are stuck with using a few lines of PowerShell:

$web = Get-SPWeb "http://sitename"
$field = $web.Fields["kpiIcon"]
$field.JSLink = "~sitecollection/Style Library/custom/KPI.js"
$field.Update($true)
$web.Dispose()

Summary
Choosing the KPI value is not changed at all so is a standard radio button:
Choosing KPI

When in display or view mode the KPI is displayed as an icon:
viewKPI

Completely reusable by anybody with the ability to add columns to list or libraries – a great and simple addition to any SharePoint environment.

This entry was posted in SharePoint and tagged , , , , . Bookmark the permalink.

3 Responses to Site Column Display Templates – KPIs

  1. Pingback: Site Column Display Templates – KPIs | Latest News

  2. Nandalal Ramphal says:

    I am getting an error “the Property ‘JSLink’ cannot be found on this object

    • paylord says:

      Hello Nandalal

      I assume you mean when running the PowerShell? You can test each stage of the script by just typing $web after you have set it and $field after you have set that. In each case it should come back with details of the object. My guess is that the field name is incorrect. You must use the display name rather than the internal name for the column.

      Let me know if this solves the problem?

      Dave

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