You might have seen the Out of the box SharePoint list view web parts showing customized column formatting for fields like status, priority and approval etc. The old way was to write a custom xslt template for the list view which can be used on list view properties to show customize output. This require too much custom effort and can risk your existing functionality.

There is another way to customize the list view web parts by simple script commands using JSlink, below I am sharing the code for a simple JSLink file which can be used for formatting the list column output.

(function () {
    var priorityFiledContext = {};
    priorityFiledContext.Templates = {};
    priorityFiledContext.Templates.Fields = {
        "Overall_x0020_Health": 
        {
            "View": priorityFiledTemplate
        }
    };
     
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(priorityFiledContext);
})();

function priorityFiledTemplate(ctx) {
    var priority = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];
   switch (priority) {
        case "Red":
                                             return "<span style='width:50px; background-color: red'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>"
            break;px
        case "Yellow":
                                             return "<span style='width:50px; background-color : yellow'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>"
            break;
        case "Green":
                                             return "<span style='width:50px; background-color : green'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>"
    }
}

Below are the steps to add a JSlink file to list view web part.

  1. Edit the list view page, this will show the list web part in edit able mode.
  2. Open the web part properties for list view web part.
  3. Scroll down and expand the Miscellaneous node and enter the url of JSLink file as shown in below screenshot

jslink edit web part

  1. Press Ok to save the changes, and then save changes on the page and you can see output similar to below screenshot which showing different color codes status field.

JSlink column formatting

You can also add image icons instead of color code by modifying the JSlink script.

Adnan is six time Microsoft MVP (Since 2015) with over 16 years of extensive experience with major expertise on SharePoint, SharePoint based development, Microsoft 365, Microsoft Teams, .Net Platform and Microsoft BI. He is currently working Sr Microsoft Consultant at Olive + Goose. He is MCT Regional Lead for Pakistan Chapter since 2012. He is working on SharePoint for past 12 years and worked on different intranet/intranet solutions for private & govt. sector majorly in United states and Gulf region and have experience of working with multiple Fortune 500 companies. He is a trainer, technology evangelist and also speaks in community forums.

Leave a Reply