Welcome to a blog on how to color your SharePoint view based on column value in Office 365. We developers usually get a requirement when our customers want to highlight the view with a color based on some status of the whole item.
SharePoint has the category color field but the functionality is very different from what they expect. So what do we do now, tell them this is not going to work in SharePoint? No, we have a way. Let me show you how.
- Create a List and under that list, create a column named Status with choice values as Current and Expired.
- Open the view.
- Edit the view and use a script editor to add the following code.
Code
- <script type="text/javascript">
- #Call in the JavaScript
- function.SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function()
- {
- SPClientTemplates.TemplateManager.RegisterTemplateOverrides(
- {
- OnPostRender: function(ctx)
- {
- var statuscolor = {
-
- #Specify the status with colors here.
-
- 'Expired': '#DC143C',
-
- #Note - If you want all the status choices to show different colors,
-
- you can add them here and assign them different colors.
-
- };
-
- #Get all rows
-
- var colorrow = ctx.ListData.Row;
- for (var i = 0; i < colorrow.length; i++)
- {
- #Identify the row with status as expired and color them.
-
- var statusval = colorrow[i]["Status"];
- var rowid = GenerateIIDForListItem(ctx, colorrow[i]);
- var row = document.getElementById(rowid);
- row.style.backgroundColor = statuscolor[statusval];
- }
- }
- });
- });
- </script>
- Once you add the code, add two items to the list.
- One should have status as current which will show no effect to the row color and the other one should have status as expired whose whole row will display red color stating it has been expired as per the screen below.
Now whatever items you add, if the status equals Expired, the complete row will display in red.