Search This Blog

Tuesday, February 24, 2015

SharePoint: Color Coding SharePoint Lists

Color Coding SharePoint Lists – without custom web parts or server code deployments 

SharePoint does not have an out of the box way to set colors based on the data in a list or library. There are both third party web parts and open source projects, such as codeplex.com, that can color code lists, but these usually require installing code on the SharePoint web servers. In this article we will look at color coding lists just using the built-in Content Editor web part and some JavaScript.
Each list will need a slightly different fragment of JavaScript to set the colors.
Color coded calendars?
For other lists and libraries you will find examples below to:
  • Set color backgrounds for rows in task lists to show the status of the task
    Color coded SharePoint task list
  • Set colors in libraries to show approval status
    Color coded SharePoint task list (approval status)
  • Set the color of a single column based on its valueColor coded SharePoint list(task status)

To add the Content Editor web part and JavaScript: 
  • Display the view to color code (each view will need its own web part and JavaScript) 
  • Add a Content Editor web part 
    • Site Actions, Site Settings, Edit Page 
    • Add a Content Editor web part and move it below the calendar web part 
    • Move this web part so it is below the list you are color coding 
    • Click in the web part, click Edit, Modify Shared Web Part, and in the Appearance section change "Chrome" to "None". 
  • Add the JavaScript 
    • Click Source Editor 
    • Type or paste the JavaScript (examples below) 
Example 1: Set the background color of a row based on data in the row 
To color code a row all we need is:
  • A column to check (such as the Task Lists' status column) 
  • JavaScript to find this item in a table TD tag 
  • JavaScript to assign a style to the TD's parent row (TR) 
  • And of course, the web part described above to hold the JavaScript 
Here is the example for a task list:

<script type="text/javascript" language="javascript">
  var x = document.getElementsByTagName("TD") // find all of the TDs
  var i=0;
  for (i=0;i<x.length;i++)
  {

    if (x[i].className=="ms-vb2") //find the TDs styled for lists
    {

      if (x[i].innerHTML=="Not Started") //find the data to use to determine the color
      {
        x[i].parentNode.style.backgroundColor='white'; // set the color
      }

    //repeat the above for each data value

      if (x[i].innerHTML=="In Progress")
      {
        x[i].parentNode.style.backgroundColor='lightgreen'; // set the color
      }

      if (x[i].innerHTML=="Completed")
      {
        x[i].parentNode.style.backgroundColor='lightblue'; // set the color
      }

      if (x[i].innerHTML=="Deferred")
      {
        x[i].parentNode.style.backgroundColor='lightgrey'; // set the color
      }

      if (x[i].innerHTML=="Waiting on someone else")
      {
        x[i].parentNode.style.backgroundColor='orange'; // set the color
      }

    }

  }
</script>

Code Notes:
  • x[i] is one of the table cells (TD) 
  • x[i].innerHTML is the contents of a cell (TD) (which may include additional HTML) 
  • x[i].parentNode is the row containing the cell (a TR) 
  • x[i].parentNode.style.stylename is used to set any valid style on the TR 

Example 2: Set the color of a field based on data in the field 
To color code a cell all we need is:
  • A column to check (such as the Task Lists' status column or a library's approval status column) 
  • JavaScript to find this item in a table TD tag 
  • JavaScript to assign a style to the TD 
Here is the revised JavaScript fragment for a color coding a single cell:
if (x[i].className=="ms-vb2") //find the TDs styled for lists
{
  if (x[i].innerHTML=="Completed") //find the data to use to determine the color
  {
    x[i].style.backgroundColor='darkblue'; // set the background color
    x[i].style.color='white'; //set the font color
  }

2 comments:

  1. exa 2 provides nothing!

    ReplyDelete
  2. Thank you for answering this important question! However, I can't seem to get it to work. When I add this code to a content editor web part by editing the source, SharePoint strips out most of it, just like it does when I try to add style code this way. Since that didn't work, I tried saving it as a txt file instead, saving it to my Site Assets library, and referencing it in the Web part instead, but I don't see any effect.

    All I changed in your code was to replace the text values (Completed, Deferred, Waiting on Someone Else, etc.). Any suggestions?

    ReplyDelete