How to add color to a SharePoint 2007 calendar

For SharePoint 2010 – go to the end of this article.

 

This article was written by a friend and colleague of mine, Richard Granger. In it, he explains an approach to making calendar events color coded inside SharePoint.

I have run into situations where I would like to differentiate calendar events by color.  For instance if I have a leave calendar I want all vacation to be colored red, sick leave colored black, travel colored green, and so on.  After doing some research I found that this can be accomplished with sharepoint out of the box using some calculated columns and a content editor web part.

Step 1 – Setting up your calendar

Start by navigating to the calendar that you are working with and access the calendar settings.  Add a column to the list that will be used to differentiate the calendar events.  I recommend using a choice column.  For example if you are working with a leave calendar you could add a column called Leave Type with choices of: vacation, sick, training, telework, travel, etc…

Step 2 – Create calculate columns

While still in the calendar settings we will want to add two more columns.  The first column will be called ‘Color’ and will be calculated column.  The following formula will set the value of the Color column based off of the value selected in the Leave Type column.  Obviously you will want to change all of the [Leave Type] text to the field you created in step 1.  Also you can play with the colors that you desire.  For instance I use “Green” but “DarkGreen” is also an option.  Feel free to try if it does not recognize the color you will know because it will not render properly.  Also in my example I have eight choices and if you do not require that many options its not a problem.  But if you have more than eight options and try to nest another IF statement sharepoint will not accept it.  If you do need to have more than eight options follow the logic in the second formula.

=IF([Leave Type]=”Leave Without Pay”,”Olive”,IF([Leave Type]=”Annual”,”Red”,IF([Leave Type]=”Sick”,”Black”,IF([Leave Type]=”Travel”,”Green”,IF([Leave Type]=”Training”,”DarkBlue”,IF([Leave Type]=”Telework”,”DarkCyan”,IF([Leave Type]=”Compensatory Time Off”,”DarkRed”,IF([Leave Type]=”Other Paid Absence”,”Purple”,””))))))))

=IF([Status]=””,”Black”,IF([Status]=”Choice1″,”Red”,IF([Status]=”Choice2″,”Gold”,IF([Status]=”Choice3″,”Green”,IF([Status]=”Choice4″,”DarkBlue”,IF([Status]=”Choice5″,”DarkCyan”,IF([Status]=”Choice6″,”DarkRed”,IF([Status]=”Choice7″,”Gray”,””))))))))&IF([Status]=”Choice8″,”MediumSlateBlue”,IF([Status]=”Choice9″,”SpringGreen”,IF([Status]=”Choice10″,”MidnightBlue”,IF([Status]=”Choice11″,”Sienna”,IF([Status]=”Choice12″,”SlateGray”,IF([Status]=”Choice13″,”OliveDrab”,IF([Status]=”Choice14″,”Gray”,””)))))))

The second column will be called ‘Display’ and will also be a calculated column.   Copy and paste the following text into the formula.

=”<span style=’position:relative;display:inline-block;width:100%;’><span style=’width:100%;display:inline-block;text-align:center;border:1px solid “&Color&”;position:absolute;color:”&Color&”;’> “&Title&”</span><span style=’display:inline-block;width: 100%;background-color:”&Color&”;text-align:center;border:1px solid;z-index:-1;filter:alpha(opacity=20);opacity:0.2;’>”&Title&”</span></span>”

The ‘Display’ column is colorizing the Title column.  You can use the ‘Display’ column to colorize any other column simply by replacing Title with the column name of your choice.

Step 3 – Set up calendar views

Staying on this page click on the Calendar view at the bottom of this page.  On the view page change the ‘Month View Type’, ‘Week View Type’, and ‘Day View Type’ from ‘Title’ to ‘Display’.  Now go back to the calendar view and you will see that you get a nasty looking display for the calendar items which we will fix in step 4

Step 4 – Fix the item display

  • Go to your calendar view and switch the web page to edit mode.
  • Click add web part and select the content editor web part.
  • Drag and drop the calendar view above the content editor web part (this is important)
  • in the Content Editor Web Part, click on “open the tool pane”
  • Click on the Source button and paste the following text into the window.
   1: <script type="text/javascript">

   2: function TextToHTML(NodeSet, HTMLregexp) {

   3: var CellContent = "";

   4: var i=0;

   5: while (i < NodeSet.length){

   6: try {

   7: CellContent = NodeSet[i].innerText || NodeSet[i].textContent;

   8: if (HTMLregexp.test(CellContent)) {NodeSet[i].innerHTML = CellContent;}

   9: } 

  10: catch(err){}

  11: i=i+1;

  12: }

  13: }

  14:  

  15: // Calendar views

  16: var regexpA = new RegExp("\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*");

  17: TextToHTML(document.getElementsByTagName("a"),regexpA);

  18:  

  19: // List views

  20: var regexpTD = new RegExp("^\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*$");

  21: TextToHTML(document.getElementsByTagName("TD"),regexpTD);

  22:  

  23: </script>

  24:  

Once you click OK on the tool pane and you should now see that your calendar is displaying color coded events.

References

HTML Color Chart.  Click on a color and the name will be shown.  You can use that color name in the equation above.

RGB Color Chart.  You cannot use these colors by name in the formula above but can reference their RGB value.

 

A big thanks to Richard for taking the time to share this with everyone!  Thumbs up

Ninja

 

UPDATE – SHAREPOINT 2010

If you want to do color coding in SharePoint 2010, use the Calendar Overlay feature. I found a great blog describing how to do it: http://bitwizards.com/Blogs/Mylene-Biddle/May-2012/Creating-Color-Coded-Calendar-Events-in-SharePoint

Took me about 5 minutes to do and, since it is out of the box capability, it is fully supported. Easy!

Advertisements

About lelandholmquest

After serving in the Navy as a Reactor Operator on fast attack submarines, I earned both a Bachelor's and Masters in Information Technology from American InterContinental University and am currently working on my doctorate. I have a beautiful wife and two of the sweetest daughters a man could ask for. And I work for the greatest company: Microsoft. At Microsoft I work on the knowledge management system for Microsoft Services: Campus on Office 365.
This entry was posted in SharePoint Coding, SharePoint Content Management Tips and tagged , , , . Bookmark the permalink.

30 Responses to How to add color to a SharePoint 2007 calendar

  1. Victor J says:

    Having an issue with the Display script; I’m receiving a Syntax error. Any help would be appreciated.

  2. Gus says:

    replace word ” & ‘ with non textual ” & ‘ … however I am still not getting color.

  3. Gus says:

    I got it to work and its brilliant. Basically there were 2 things to change basically Step 3: you have to replace the ” & ‘ with the ” & ‘ from notepad (not textual symbols)…

    And the Step 4: I had to remove the numbering that was in the script. i.e.

    function TextToHTML(NodeSet, HTMLregexp) {
    var CellContent = “”;
    var i=0;
    while (i < NodeSet.length){
    try {
    CellContent = NodeSet[i].innerText || NodeSet[i].textContent;
    if (HTMLregexp.test(CellContent)) {NodeSet[i].innerHTML = CellContent;}
    }
    catch(err){}
    i=i+1;
    }}
    // Calendar views
    var regexpA = new RegExp("\\s*\\s*");
    TextToHTML(document.getElementsByTagName("a"),regexpA);
    // List views
    var regexpTD = new RegExp("^\\s*\\s*$");
    TextToHTML(document.getElementsByTagName("TD"),regexpTD);

  4. Peter De Rop says:

    Hm.. I can’t get this thing to work. The calendar items keeps showing up <span style=… I tried adding an alert to the script — it doesn't show up.

    • Austin says:

      Hi, Thanks for this great walkthrough. I had found this solution on a few other sites but could not follow them until your site. I did run into the same problems listed above where even after everything I was still plagued by the “display” code appearing in my calendar. I fixed it by rereading the instruction in step 4. I had placed my content web editor above the calendar. For whatever reason (coders you could explain) just moving it solved all my problems.

      Goodluck out there.

  5. rayvelynd@hotmail.com says:

    Was anyone successful using Gus’s fix?… I am not able to get it to work.

  6. Kevin Wolfe says:

    My calendar displays with color coded entries (yes!) but the dates around the page display as ‘undefined’, eg the dates down the LHS where it would show the week (or blank) is now displaying the word undefined. http://i1307.photobucket.com/albums/s581/aussiemuscle308/work%20pictures/calendar_undefined_zps459e028c.jpg

  7. Accutane says:

    I have been reading out many of your articles and i can claim pretty nice stuff. I will make sure to bookmark your blog.

  8. Pingback: Autoapprove List

  9. I love looking through a post that can make people think.

    Also, many thanks for permitting me to comment!

  10. Heya i am for the first time here. I found this board and I in finding It truly useful & it helped me out a lot. I am hoping to give one thing again and help others like you aided me.

  11. I drop a comment each time I especially enjoy a article on a website or I have something to add to the discussion. Usually it’s caused by the fire displayed in the post I read. And on this article How to add color to a SharePoint 2007 calendar | Leland&. I was actually moved enough to leave a thought 😛 I actually do have a few questions for you if it’s okay. Could it be only me or do a few of these responses appear as if they are left by brain dead individuals? 😛 And, if you are writing at other social sites, I’d like to follow everything fresh you have to post. Could you list the complete urls of your shared sites like your Facebook page, twitter feed, or linkedin profile?

  12. I believe what you said made a lot of sense. However, think about this, what if you added a little information? I mean, I don’t want to tell you how to run your blog, however what if you added a post title to possibly get a person’s attention? I mean How to add color to a SharePoint 2007 calendar | Leland& is a little boring. You ought to peek at Yahoo’s home page and see how they create news titles to get people to click. You might try adding a video or a pic or two to grab people excited about what you’ve written. Just my opinion, it might make your posts a little bit more interesting.

  13. Excellent submit. I merely stumbled upon your website along with wanted to state that We have actually loved looking your site articles. No matter the reason I am registering to your current supply and that i hope you create again before long!

  14. Don says:

    I got it working using Gus’ fix

  15. london says:

    Please help – I love this and managed to get to Step 4 but then failed 😦 I get the Calender up but every item just shows the <span style='…….code

    Any help at all please…..my source editor code is below:

    function TextToHTML(NodeSet, HTMLregexp) {
    var CellContent = “”;
    var i=0;
    while (i < NodeSet.length){
    try {
    CellContent = NodeSet[i].innerText || NodeSet[i].textContent;
    if (HTMLregexp.test(CellContent)) {NodeSet[i].innerHTML = CellContent;}
    }
    catch(err){}
    i=i+1;
    }}
    // Calendar views
    var regexpA = new RegExp("\\s*\\s*”);
    TextToHTML(document.getElementsByTagName(“a”),regexpA);
    // List views
    var regexpTD = new RegExp(“^\\s*\\s*$”);
    TextToHTML(document.getElementsByTagName(“TD”),regexpTD);

  16. Cindy says:

    Is there another option in order to modify the code. I don’t exactly understand it. I don’t have Notebook and it did not work in Notepad.

  17. Fee says:

    Hi, im also getting the syntax error when trying to create inserting the formula for the Display column. I have copied and pasted the formula into Note pad and replaced all of the “& yet im still getting an error. Can anyone provide a step by step how to resolve. I really would like to use colour on the calendar

  18. Leeza says:

    Hi folks,

    I just tried this out and after using the tips inthe comments, was able to get the colors onthe events, however I am seeing the same “undefined” on the LHS of the calendar in month view and at the top in the day and week view. Is there a resolution for this?

  19. Neal Lu says:

    I’ve also managed to make it work via Gus’s fix. Some things to keep in mind: for step 4 (source editor code), make sure you include the entire code (including the “<script type…" and "” pieces). As far as the syntax error in step 3, make sure you replace all ” and ‘ (both double and single apostrophes) in notepad. For reference, the full script I used for step 4 is below.

    function TextToHTML(NodeSet, HTMLregexp) {
    var CellContent = “”;
    var i=0;
    while (i < NodeSet.length){
    try {
    CellContent = NodeSet[i].innerText || NodeSet[i].textContent;
    if (HTMLregexp.test(CellContent)) {NodeSet[i].innerHTML = CellContent;}
    }
    catch(err){}
    i=i+1;
    }
    }

    // Calendar views
    var regexpA = new RegExp("\\s*\\s*”);
    TextToHTML(document.getElementsByTagName(“a”),regexpA);

    // List views
    var regexpTD = new RegExp(“^\\s*\\s*$”);
    TextToHTML(document.getElementsByTagName(“TD”),regexpTD);

    • Neal Lu says:

      Sorry, the “script” pieces didn’t show up the first time… use the below but remove the ” from the beginning and end.


      function TextToHTML(NodeSet, HTMLregexp) {
      var CellContent = “”;
      var i=0;
      while (i < NodeSet.length){
      try {
      CellContent = NodeSet[i].innerText || NodeSet[i].textContent;
      if (HTMLregexp.test(CellContent)) {NodeSet[i].innerHTML = CellContent;}
      }
      catch(err){}
      i=i+1;
      }
      }

      // Calendar views
      var regexpA = new RegExp("\\s*\\s*”);
      TextToHTML(document.getElementsByTagName(“a”),regexpA);

      // List views
      var regexpTD = new RegExp(“^\\s*\\s*$”);
      TextToHTML(document.getElementsByTagName(“TD”),regexpTD);

      • Neal Lu says:

        EDIT: Seems the site automatically removes the script pieces.. use the below but add around the first and last lines.

        script type=”text/javascript”
        function TextToHTML(NodeSet, HTMLregexp) {
        var CellContent = “”;
        var i=0;
        while (i < NodeSet.length){
        try {
        CellContent = NodeSet[i].innerText || NodeSet[i].textContent;
        if (HTMLregexp.test(CellContent)) {NodeSet[i].innerHTML = CellContent;}
        }
        catch(err){}
        i=i+1;
        }
        }

        // Calendar views
        var regexpA = new RegExp("\\s*\\s*”);
        TextToHTML(document.getElementsByTagName(“a”),regexpA);

        // List views
        var regexpTD = new RegExp(“^\\s*\\s*$”);
        TextToHTML(document.getElementsByTagName(“TD”),regexpTD);

        /script

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s