Archive

Archive for March, 2015

SharePoint 2013: Getting lookup column values in JSLink

Hi Friends,

While working on of the development requirement related to JSLink I came across a scenario where a lookup value needed to be shown in a list view web part.

I wanted to demonstrate with following code how we can achieve to show the lookup values form single valued or multi valued lookup.

Before that I would like to show the relationship between the lists I used to show the demonstration.

I have created following two custom lists for demonstration.

First list with following column –

TestList2

 

The content in First list will like something –

TestList2Content

Second list having lookup column from first list –

TestList1

 

The content in second list like following –

TestList1Content

Following code will show how to get the single or multi valued lookup values and show.


(function () {

var overrideCtx = {};
overrideCtx.Templates = {};
overrideCtx.Templates.Item = CustomPortalPageItem;
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
})();
function CustomPortalPageItem(ctx) {
var html = "";
html += '<div title="' + ctx.CurrentItem.Title + '"></div><div>'+GetLookupValues(ctx.CurrentItem.Test11,"lookupValue")+'</div>';
return html;
}

function GetLookupValues(inputArry,originalColumnName)
{
var retHtml="";
if(inputArry!=null)
{
for(var loopcnt=0;loopcnt<inputArry.length;loopcnt++)
{
if (typeof(inputArry[loopcnt][originalColumnName]) != 'undefined' && inputArry[loopcnt][originalColumnName] != null)
{
retHtml+=inputArry[loopcnt][originalColumnName];
}
}
}
return retHtml;
}

After this it will look like following,you can then apply beautiful HTML to it.
JSLInkOP

Advertisements