How To Make SharePoint Field Data As Hyper Link Using JS Link

JS Link

JS Link property is a new addition in SharePoint 2013. This is a great feature with which users can control  the rendering of fields, items, and even Web Parts, using a JavaScript File (referenced in JS Link property field of a Web Part).

This example will explain how we can make SharePoint fields as hyperlinks based on the other fields. In the previous versions of SharePoint, we used to create this using calculated field. Calculated column sometime gives us a problem in displaying the HTML content (even if we add it as a type number).

JS Link provides elegant ways for accessing the SharePoint fields and customizing in the client side to display the client View.

Modifying Field to Hyperlink

In the below example, I have created sample list RK_TestList. In this, I have created Name, Address, and Specialization fields. My requirement is to navigate to the selected user profile on clicking on the name, and the name should display by combining the address location.

SharePoint List

Add some sample information to the list.

Edit the page and add web part.

Select "Script Editor" under "Media and Content" category.


Click on the "Edit Snippet" link. This opens the popup where we can add our custom JS code.

Add the below code to generate the links.



In this code, I am overriding the existing name field data with my custom data; this code also tells us how we can access the list item fields.

JavaScript Code 

  1. <script type="text/javascript">  
  2. (function () {  
  3.     // Create an object that have the context information about the fields that we want to change the rendering of.   
  4.     var nameFiledContext = {};  
  5.     nameFiledContext.Templates = {};  
  6.     nameFiledContext.Templates.Fields = {  
  7.         // Apply the new rendering for these fields on List View  
  8.         "Name": { "View": nameFiledTemplate }  
  9.     };  
  10.     SPClientTemplates.TemplateManager.RegisterTemplateOverrides(nameFiledContext);  
  11. })();  
  13. // This function provides the rendering logic for the list view  
  14. function nameFiledTemplate(ctx) {  
  15.     var name = ctx.CurrentItem.Name;  
  17.     var address = ctx.CurrentItem.Address;   
  18.     if(address == "Hyderabad"){  
  19.         address = "HYD";  
  20.     }  
  21.     else{  
  22.         address = "BAN";  
  23.     }  
  25.     return "<a target='_blank' href=''>"   
  26.         + name + " - " + address + "</a>";      
  27. }  
  28. </script>   

Stop editing the page. Now, you can see the result.

The name field shows as hyperlink and navigates to the user profile site based on our custom code functionality and also, we are displaying the address along with name.


Hope this article will help you in understanding the JS Link concept and its usages in the project.

Happy Coding!!!

Up Next
    Ebook Download
    View all
    View all