Get Column Values in JSLink Using SharePoint

When working on the development requirements 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 show with the following code how to show the lookup values from a 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 the following two custom lists for demonstration.

The first list has the following columns.



The content in the first list will be something like:



The second list has the lookup column from the first list.



The content in the second list is like the following.



The following code will show how to get the single-valued or multi-valued lookup values and show them.

  1. (function()   
  2. {  
  3.   
  4.     var overrideCtx = {};  
  5.     overrideCtx.Templates = {};  
  6.     overrideCtx.Templates.Item = CustomPortalPageItem;  
  7.     SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);  
  8. })();  
  9.   
  10. function CustomPortalPageItem(ctx)  
  11. {  
  12.     var html = "";  
  13.     html += '<div title="' + ctx.CurrentItem.Title + '"></div><div>' + GetLookupValues(ctx.CurrentItem.Test11, "lookupValue") + '</div>';  
  14.     return html;  
  15. }  
  16.   
  17. function GetLookupValues(inputArry, originalColumnName)   
  18. {  
  19.     var retHtml = "";  
  20.     if (inputArry != null)   
  21.     {  
  22.         for (var loopcnt = 0; loopcnt < inputArry.length; loopcnt++) {  
  23.             if (typeof(inputArry[loopcnt][originalColumnName]) != 'undefined' && inputArry[loopcnt][originalColumnName] != null) {  
  24.                 retHtml += inputArry[loopcnt][originalColumnName];  
  25.             }  
  26.         }  
  27.     }  
  28.     return retHtml;  
  29. }  
After this it will look like the following. You can then apply beautiful HTML to it.


 

Up Next
    Ebook Download
    View all
    Learn
    View all