SharePoint 2010 Colour Code Calendar

SharePoint 2010 Colour (color) Calendar

Following a number of requests I have produced a first attempt at a colour calendar solution for SharePoint 2010.
SP2010 does offer a form of colouring for calendars but only to allow different colours for overlaid calendars. The other change introduced in SP2010 is that the calendar itself is a mixture of rendered mark-up sent to the client with the actual events rendered at runtime in JavaScript.
We are going to use a similar mechanism to that employed in the other colouring solutions I have produced in that we will colour the elements in client side script once the calendar has loaded. Initially we will just hard code the colour values in the JavaScript file; a future post will cover making Client Object Model calls to retrieve the colouring values from a list so we can reuse the colours across many calendars. This will also use JQuery to simplify the manipulation of the HTML.
For now though we will concentrate on the colouring of a single calendar view. We are going to utilise two script files, the first is JQuery available from jquery.com and the second we will write ourselves to contain the colouring script. Download the latest JQuery minified file, at time of writing this should be a file named jquery-1.4.2.min.js
Copy the following code and paste into a file. I have named mine PlanetWilson2010Cal.js file

_spBodyOnLoadFunctionNames.push('WaitForCalendarToLoad'); var SEPARATOR = "|||"; function WaitForCalendarToLoad() { // we will slightly rewrite this existing function as defined originally in SP.UI.ApplicationPages.Calendar.debug.js var pwold$4a = SP.UI.ApplicationPages.CalendarNotify.$4a SP.UI.ApplicationPages.CalendarNotify.$4a = function () { pwold$4a(); ColourCalendar(); } } function ColourCalendar() { if($('a:contains(' + SEPARATOR + ')') != null) { $('a:contains(' + SEPARATOR + ')').each( function (i) { $box = $(this).parents('div[title]'); var colour = GetColourCodeFromCategory(GetCategory(this.innerHTML)); this.innerHTML = GetActualText(this.innerHTML); $($box).attr("title", GetActualText($($box).attr("title"))); $box.css('background-color', colour); }); } } function GetActualText(originalText) { var parts = originalText.split(SEPARATOR); return parts[0] + parts[2]; } function GetCategory(originalText) { var parts = originalText.split(SEPARATOR); return parts[1]; } function GetColourCodeFromCategory(category) { var colour = null; switch (category.trim().toLowerCase()) { case 'meeting': colour = '#4FDB51'; break; case 'work hours': colour = '#4FB8DB'; break; case 'business': colour = "#F08616"; break; case 'holiday': colour = "#F55875"; break; case 'get-together': colour = "#E0F558"; break; case 'gifts': colour = "#F558D5"; break; case 'birthday': colour = "#6E80FA"; break; case 'anniversary': colour = "#FF4040"; break; } return colour; } 

Add these two files to a document library in your site. I have added them to a library called “shared documents” in the root of the site collection. You can add them where you want but be sure to update the script references below accordingly.
1
Next edit the calendar view page to add a content editor web part. You should be able to just choose Site actions -> Edit Page to get into edit mode. Then add a content editor web part above the calendar view. Paste the following code into the web part:-

<script type="text/javascript" src="/Shared Documents/jquery-1.4.2.min.js"></script><script type="text/javascript" src="/Shared Documents/PlanetWilson2010Cal.js"></script>

1b
Be careful here as I find the behaviour of the CEWP to be a little unpredictable and it sometimes strips out my script tags. I haven’t got to the bottom of this yet and it may be a particular scenario under which it occurs.
Now we need to actually get the calendar to output correctly so our colour coding works. On the calendar screen now click on the List Settings button. This will take you to the list settings screen. We need to create a new calculated column called CategoryTitle. When prompted for the formula use the following:-
= “|||” & [Category] & “|||” & [Title]
2
Back on the list settings screen, click to edit the Calendar view and choose the Month View, Week View and Day View under Calendar Columns to all be your new CategoryTitle field.
4
Now when you go back to the main calendar you should see any events you create are colour coded!
5
If you wish to change the default categories on the list then do so but you will need to edit the colouring script to add new CASE statements in the last function GetColourCodeFromCategory(). Be careful to ensure the categories you add here are put into lowercase as the script lowercases everything for comparison.

About these ads
This entry was posted in Sharepoint Server 2010. Bookmark the permalink.

One Response to SharePoint 2010 Colour Code Calendar

  1. Roch says:

    Hello Arun,
    Thanks very much for this post, this is exactly what I need!
    However I am not able to make it work. This is probably because I replaced the column ‘Category’ by the column that I want and this column has a blank space (Bulletin Type). So I put what I feel is the internal name i.e. Bulletin_x0020_Type but I not so sure. How can I find the exact column name to put in the JavaScript?
    Best Regards
    Roch

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