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, a distinguished professional, boasts an impressive track record as a Microsoft MVP, having achieved this prestigious recognition for the eighth consecutive year since 2015. With an extensive career spanning over 18 years, Adnan has honed his expertise in various domains, notably excelling in SharePoint, Microsoft 365, Microsoft Teams, the .Net Platform, and Microsoft BI. Presently, he holds the esteemed position of Senior Microsoft Consultant at Olive + Goose. Notably, Adnan served as the MCT Regional Lead for the Pakistan Chapter from 2012 to 2017, showcasing his leadership and commitment to fostering growth within the tech community. His journey in the realm of SharePoint spans 14 years, during which he has undertaken diverse projects involving both intranet and internet solutions for both private and government sectors. His impact has transcended geographical boundaries, leaving a mark on projects in the United States and the Gulf region, often collaborating with Fortune 500 companies. Beyond his roles, Adnan is a dedicated educator, sharing his insights and knowledge as a trainer. He also passionately advocates for technology, frequently engaging with the community through speaking engagements in various forums. His multifaceted contributions exemplify his dedication to the tech field and his role in driving its evolution.

Leave a Reply