ATTENTION!
This article has been superceded by an updated deployable version which be found here.
The in-built calendar view is SharePoint 2007 is a heck of a lot better than the one in 2003 but it still lacks a feature requested by many customers - the ability to colour (or color for our US friends) code the entries like you can in Outlook.
I had previously created a coloured calendar in 2003 and started to investigate how this might be achieved in 2007. Whilst there are commercial alternatives avilable, the licencing models can be cumbersome - tied to a physical machine etc which make development and deployment a pain.
(Update: if you want to know about the 2003 version, click here)
In SharePoint 2003 the calendar was rendered entirely using Javascript so it was a question of modifying the schema.xml file for the list to output the relevant parameters and then overiding an ows.js function to ensure the colours got rendered. However in 2007 the calendar view is generated from code, specifically a bunch if classes like SPListView, SPCalendarView etc. Lots of this class heirarchy is marked as sealed or internal so it is not easy to try to inherit and change the behaviour. Plus lots of the code is obfuscated too.
As an alternative approach I began to look at perhaps manipulating the calendar in Javascript once it had been delivered to the browser (thanks to Todd Bleeker for inspiration here)
First of all I created a standard SharePoint Calendar list. Then I added a Choice column called Category and put the following values in the choice field.
Appointment
Birthday
Business
Important
Vacation
Secondly I created a calculated column called CatTitle set to the following formula as seen in the image,
=Category & "|||" & Title
You could make these site columns if you wish to re-use elsewhere.
Finally change the calendar view on the list so that the display field for each of day/week/month view is the CatTitle field.
Now we need to insert some javascript to edit the titles so that they category and pipes are removed and the entries are coloured in. Edit the calendar page (Site Actions -> Edit Page) and insert a content editor web part UNDER the calendar list view. Paste the following javasript into the source view. Make the web part hidden.
<script>
var SEPARATOR = "|||";
var nodes, category;
nodes = document.getElementsByTagName("a");
for(var i = 0; i < nodes.length; i++)
{
if(nodes[i].innerText.indexOf(SEPARATOR) != -1)
{
UpdateCalendarEntryText(nodes[i]);
var foundNode = nodes[i];
var trap = 0;
while(foundNode.nodeName.toLowerCase() != "td")
{
foundNode = foundNode.parentNode;
trap++;
if(trap > 10)
{
break; // don't want to end up in a loop
}
}
var colour = GetCalendarColour(category);
if(colour != "")
foundNode.style.background = colour;
}
}
function UpdateCalendarEntryText(anchorNode)
{
var children = anchorNode.childNodes;
for(var i = 0; i < children.length; i++)
{
if(children[i].nodeType == 3 && children[i].nodeValue.indexOf(SEPARATOR) != -1)
{
var parts = children[i].nodeValue.split(SEPARATOR);
category = parts[0];
children[i].nodeValue = parts[1];
}
else
UpdateCalendarEntryText(children[i]);
}
}
function GetCalendarColour(desc)
{
var colour;
switch(desc.toLowerCase())
{
case "appointment":
colour = "#ffd266";
break;
case "birthday":
colour = "#ae99dc";
break;
case "business":
colour = "#8aabe0";
break;
case "important":
colour = "#e77379";
break;
case "vacation":
colour = "#fffa91";
break;
default:
colour = "";
}
return colour;
}
</script>
You should now have a coloured calendar! Month, week and day views should appear coloured. Remember that if you place a calendar web part on a site that you will need a hidden content editor web part containing the javascript as well.
If you want to change the categories and colours, then simply add or change entries in the choice column and change the function in the Javascript accordingly.
In an ideal world this would be some sort of deployable feature, perhaps custom list definition. I will have a look at doing this at some point if I get time....
45 comments:
Awesome idea and writeup Mark, thanks for sharing that! SharePoint keeps getting bigger and better, but I think the CEWP will always have a place.
WOW!! That is awesome!!! Thanks!!!
I believe to have followed your instructions closely. However, I do not see any colors and entries display as
Birthday|||whatever
any tips?
P. Rodriguez USA
The content editor web part must appear after the calendar web part on the page, have you confirmed that this is the case?
Otherwise can you view source on the page and email it to me?
Regards,
Mark
I had the same issue when cut and past code
need to layout text as displayed on your page
Anonymous is correct. The line breaks must be respected. Pasting into the web part's Rich Text Editor will not work. So upon pasting into the Source Editor, manually press enter at the begining of each line. The indenting is irrelevant.
Great idea Mr. Wilson, I have been looking for a solution like this for MONTHS, Thanks a bunch!!!
excellent post!!worked on first try.
Hint: I copied the js code into notepad and then copied this text and used it in the content editor WP. Easy way to remove any formatting. (instead of hitting enter after each line)
I am not able to see the color. The webpart is after the calendar webpart. I also pasted the code in the note pade first and then copied in the source editor.In addtion, I pressed Enter after each line.
Can I help you the source code? If yes, what is your email address?
You can email me at blog@planetwilson.co.uk
I think the code was great..if you have issues getting ot to work..most likely the error is from cutting and pasting of the code ...happened to me and I realised there was some funny format issue
I had the same problem with no colour and entries displaying as Appointment|||Whatever. If you're a newbie like me, you may have added the content editor webpart on the site's main page. what you actually need to do is select the calendar, then click "Edit Page", and add the content editor webpart exactly as described, ie under the calendar list and make it hidden. Then it works great!Awesome code, thanks for sharing it!
Hi,
is the script working on WSS3.0 too?
There is no reason why it wouldn't work exactly the same on WSS
Thankyou very much for this post! It is greatly appreciated.
Just thought id post a quick mention that when i posted the code into notepad it messed up the formatting - posting it into wordpad seemed to sort it though :)
Mark
Brilliant - worked first time...
I'm using the German version of WSS 3.0 and followed exactly the instructions.
However, it doesn't work unfortunately as I'm getting the "Birthday|||Paul" entry as well.
Is there something to customize due to teh German language? If so, any idea what?
thanks!
You do need to ensure that the content editor web part appears after the calendar web part. Otherwise I can't say I have tried it on a German language site. If you email me the IE source I'll see if I can spot what is wrong.
The mistake I made was to try to add the script via the RTE. It works fine now, thanks for the quick help!
This is brilliant. Any other cool javascript tips?
cheers!!!
Love it!!!!
Love it!!!
I still get Birthday|||whatever and
I notice the space in the formula
for CalTitle has changed
from =Category & "|||" & Title
to =Category&"|||"&Title.
Do you think this is the problem ?
If so, how do you make the space stay in =Category & "|||" & Title
which is what I typed in ?
I'm using SharePoint server 2007.
The spaces don't matter, have you placed the web part with the javascript on the same page s the calendar AFTER the calendar web part?
If you are still having problems hen email me with the source of the page.
Is it possible for this technique to automagically grab/read the contents of the field being used for coloring and then assign the colors to the values??
Reason I ask is one might want to colorize a field where new values are added from time to time, which in the way this script is written requires manual intervention to add a value/color pair.
Thank you, tom
I am working on a more robust solution deployable version of the colour calendar which wil have a centrally managed list of colour categories to get around the problem you mention. I'll post when it is finished which I hope won't be too long now.
Mark
Mark,
Please disregard my post to you yesterday regarding how to add the
content editor web part after the calendar web part. I've just learned to move the content web part after the calendar web part.
I'll try that to make it work. Thank you for your wonder JS for changing calendar color.
This is FANTASTIC!! Thanks so much for posting!!
An Outstanding workaround, proving once again that while microsoft may have some great programmers, sometimes it takes necessity & imagination as well.
Anyone know of a good foreground color changer?
Thank you so much for letting us use this.
Can you please point me where I can change categories on the server so that when a new calendar is created, it has my custom categories by default?
You will need to use the updated version to do what you mention, please see the top of the article for a link to the new version.
Hi!
Great work but I still have an issue. If I want to add two or more different calendars with differnet color maps and categories how can I manage this with your calendar.
I´ve tried it for hours and it seems that I allways get this one and first categorie list. ????
Cheers Pete
There is an updated version of this calendar if you read the top of the post. However both methods don't support having different colour coding on one screen. I will look at updating it to support this.
Hi all,
great post! Thank you very much! Coloring works perfectly with IE, but does andybody get it to work with Firefox? In my case it simply does....nothing, with Firefox. Do I have to just accept that or is there a fix?
Thanks a lot!
Andreas
The script needs a fix, the latest updated version linked to at the top of the page does work with Firefox
How do I get the new javascript that supports the calendar color coding on firefox? I only need the javascript. Where is it exactly? Thanks
The javascript is built into the web part in the new version of the solution. You would need to install it somewhere and grab it from the source view.
Excellent, Thanks
Great job! Thanks a lot!
Rainer
You might want to take a look at the the new version 2 which is a lot better than this solution...
http://planetwilson.blogspot.com/2008/02/version-2-of-colour-calendar-released.html
Works great!
Mark -> how do I customize the names & color options?
Hello, brilliant solution. I just installed the version 2.2 and it works fine. Now I wanted to change teh category names and colours, but I did not see a hint how to do that. Can you advice me. Thanks.. Maybe I am blind..
You need to change the site column defined in site settings on root site -> site columns. Add new categories, push these down to lists. Then edit the colour category mapping list in the root site to update the colours for each category,
***NOTE****
To get the original version to work.. you HAVE to make sure that you change the Calendar Views to CatTitle in the CURRENT VIEW of the webpart on the main page (Calendar->edit->Modify Shared Web Part->List Views->Edit the current view). Do NOT click on the actual Calendar list to make the changes.
:)
beautiful solution...works great. i did have to make sure the JS was properly formed with line breaks, etc. otherwise it would not work. Geoff
There is a new deployable version, check out later posts
Post a Comment