Showing posts with label SharePoint Calendar. Show all posts
Showing posts with label SharePoint Calendar. Show all posts

2/20/2011

SharePoint 2010: Color Coded Calendars (updated!)

 

As I’m working on my customization book I am revisiting every one of my past “quick tips”. As I do so I am often finding a better way to do something. I ran into some issues working around the asynchronous loading of calendar events with the article below. After digging more into the code behind the calendar pages in 2010 I have found a much better way to trigger the color coding code, which fixed a few other issues along the way. 

 

The original article, with the updates is here: http://techtrainingnotes.blogspot.com/2010/06/sharepoint-2010-color-coded-calendars.html

 

(An article may be written and published, but seems it is never finished!)

.

1/22/2011

Book Update

 

Update! The book’s done! 

See here for the update and here for more about the book.

 

 

I made a big mistake during my presentation at the Columbus SharePoint Saturday… I mentioned that I was working on a book and that it would soon be available.  What was that about “the best-laid plans of mice and men?” As an instructor I get to teach up to forty hours a week, and each classroom hour requires three to eight hours of prep time. Over the last few months I’ve had a heavy class load and little free time for the book. (I keep letting work get in the way of hobbies and book writing!)

Several people have been pestering me about “is it out yet?” So let me tell a little about the book and maybe why it’s taking longer than expected.

 

SharePoint® 2007 and 2010 Customization for the Site Owner

by Michael T. Smith

 

There are a lot of books for SharePoint administrators and SharePoint developers, and even a few for SharePoint end users. But there is not much available for the site owner, especially on quick and easy site customization. Over the last five years I have probably trained at nearly a thousand site owners and written a lot blog articles with tips and tricks for site owners. Most of the blog articles were from site owner questions during or after class that really made me think about how to solve the problem without writing custom code. Most of these articles have been fairly short and to the point: add this web part, copy and paste this JavaScript or CSS. Each time I have written one of these blog articles I have wished I had the time to expand the article to explain more about the how and why of the solution, or to explain the logic of how I figured out how to do it so you could create more customizations like the one in the article. So I started writing the book…

The book has these goals: take what I’ve done in the blog articles as a start and then greatly expand the number of tips and tricks, explain how the tricks work, show how they can be expanded to other uses, and provide a few chapters on basic SharePoint, HTML, CSS and JavaScript skills. The first edition of the book fully supports both SharePoint 2007 and 2010 . If you have not upgraded to 2010 yet, you will one day and will want to know how to make sure these tricks still work.

 

About the other 80%

Writing a book fits the 80 / 20 rule. The first 80% of the work takes 80% of the time, and the remaining work also takes 80% of the time.  :-(   My biggest challenge in this project is knowing when to stop. It seems I can always find just one more way to do something, one more trick or one more screen capture. Where possible I’m rewriting each tip to work in SharePoint 2007 and SharePoint 2010, and as often as possible using the same code for both. I’m also adding jQuery versions of some of the examples for those who are already working with jQuery. I’m testing every trick in Internet Explorer 6 to 8 and FireFox and rewriting the JavaScript to work with all of these browsers. (really fun, let me tell ya…)

 

Book Progress

Most of the chapters are finished and are now being proof read by some nasty critics. The cover art still needs to be done. The index needs to be created. When all of that is done, the book needs to be submitted and a test copy printed to see if everything “works”, things like margins, images, cover art, etc.

 

How it will be published

I don’t see this book ever being finished. There’s just too many things I’m learning about SharePoint everyday. Books published though the traditional channels rarely get updated and I want to update the book as needed. So I am going the “self published” route using a service owned by Amazon.com. It will be first released in print format and I hope to move the book into Kindle and other electronic formats as I get time.

 

A supporting web site

The book will have a version number!  (Should I start with a beta release?)  For that and a number of other reasons the book will also have a supporting web site. There you will find all of the source code, a number of the tricks as downloadable web parts along with any new additions. So don’t get upset if you see a version 1.1 of the book after you bought 1.0. The changes will be on the site.

 

Here’s the current, but still incomplete, table of contents:

(This will change over the next few weeks…)

 

+ Chapter 1 - Read Me First
+ Chapter 2 - An HTML Primer
+ Chapter 3 - A CSS Primer
+ Chapter 4 - A JavaScript Primer
+ Chapter 5 - Hacking Tips!
+ Chapter 8 - Web Part Must Knows
+ Chapter 9 - The Content Editor Web Part
+ Chapter 10 - User Interface Customization
+ Chapter 11 - List and Library Customization
+ Chapter 12 - Surveys
+ Chapter 13 - Links Lists
+ Chapter 14 - Task Lists
+ Chapter 15 - Calendars
+ Chapter 16 - Security Tricks
+ Chapter 17 - Sound, Video, Silverlight and Flash!

 

 

Be patient!  I working as fast as I can.    :-)

6/28/2010

SharePoint 2010: Color Coded Calendars!

An expanded version of this article, along with many other customization examples and how-tos can be found in my book, SharePoint 2007 and 2010 Customization for the Site Owner.

 

 

Updated 9/6/11

Service Pack 1 broke my color coded calendar code! For details see here. The code below has been updated to work with both the pre-SP 1 and SP 1 installations.

 

Updated 2/20/11

As I’m working on my customization book I am revisiting every one of my past “quick tips”. As I do so I am often finding a better way to do something. I ran into some issues working around the asynchronous loading of calendar events with the article below. After digging more into the code behind the calendar pages in 2010 I have found a much better way to trigger the color coding code.  The sections that have been replaced below are now crossed out (aaaaaa) and the new content added.

This update also solves most of the issues found in the comments at the end of this article, especially the “more items” problem.

 

 

Color Coded Events in SharePoint 2010 Calendars

Back in 2008 I wrote a little blog article on adding color to SharePoint 2007 calendar events. Several people have been pestering me to update it for SharePoint 2010, so here goes...

Several things have changed between SharePoint 2007 and 2010 to prevent the old example from working. For fellow “SharePoint client side hackers” I thought I would document the trail I followed to get color coding to work in 2010. For those who do not care about the details, just scroll on down to “Color Coded Events in SharePoint 2010 Calendars Step by Step”.

They broke the Content Editor Web Part!

In 2010 it’s now just called “Content Editor” and is in the “Media and Content” section of Add a Webpart:

image

 

When you add this web part you get a “wiki style” editor. Just click in the new web part and start typing. The Ribbon will then be updated to show all of the Rich Text editor options.

image

 

How do you add HTML?

You can just click the HTML button in the Ribbon (image ). But read on for some fun issues with this option…

image

But when you add CSS or JavaScript you may get this nice little message!

image

As a trivial example I entered:

  <style>
    .s4-titletext h1 a
    {
      color:red
    }
  </style>

and it reformatted it as:

  <style>

    .s4-titletext h1 a
    {
      color:red
    }</style>

and each time you go back into the HTML editor and click OK and adds another blank line after <STYLE>

  <style>

    .s4-titletext h1 a
    {
      color:red
    }</style>

It has even changed capitalization. In one of my edits it changed color:red  to  COLOR:red.

Who knows why….

 

So add your code directly to the page

Use SharePoint Designer and add the code to the bottom of the view page. The best place is most likely  between the “</WebPartPages:WebPartZone>” tag and the “</asp:Content>” tag. If you are using SharePoint Designer 2010 you will need to click “Advanced Mode” in the ribbon to edit that area.

 

Or just link to your code!

Both 2007 and 2010 offer the option to link from the CEWP to a text file with our content. So for 2010 I usually upload a text file containing the code to a library. Then in the CEWP just click the “Edit Web Part” option in the dropdown and add the link to the code file.

image

 

 

They broke the Calendar!

Not really, but is sure has changed. In 2007 the calendar was created entirely server side. The HTML sent to the browser was the final, ready to display HTML. All we needed to do was to write a little JavaScript and/or CSS to “customize” it.

Why it’s now a bit harder to code…

In 2010 the data for the calendar is now asynchronously loaded using a JavaScript function call after the page is loaded. That means that the data we want to change is not yet in the browser when our JavaScript is loaded from a Content Editor Web Part. If you have played with SharePoint client side JavaScript then you have seen the following command used to run a function after the page has loaded.

_spBodyOnLoadFunctionNames.push("_spAjaxOnLoadWaitWPQ2")

Note: _spBodyOnLoadFunctionNames is just a JavaScript array and push is a JavaScript method to add a new item to the end of the array. When a SharePoint page is loaded each function listed in this array is called in order.

I have used this often in 2007 to make sure the page is fully loaded before my JavaScript runs. The problem is where Microsoft loaded this for calendars in 2010… at the very end of the page. That means that when I use a Content Editor Web Part to add my JavaScript it will be in the “queue” before the JavaScript used to add the calendar events. So my challenge now is to get my code to run after the code that loads the calendar.

The work around?

The work around? We have to get our function added to _spBodyOnLoadFunctionNames after SharePoint's calendar update function. One way is to add our JavaScript in the master page and write the code so it only impacts calendars. But that’s a bad workaround. We would then have JavaScript that’s only needed for calendars added to every page.

So here’s another workaround… (Have a better idea? Let me know!)  Add a bit of JavaScript at the bottom of the master page that checks for an array of my functions that need to be run. If there’s no array, then do nothing…

  <script type="text/javascript">
    if (window.mikesCode)
    {
      for (var i=0;i<mikesCode.length;i++)
      { 
        _spBodyOnLoadFunctionNames.push(mikesCode[i]);
      }
    }
  </script>

Then in the Content Editor Web Parts (or anywhere else) add a custom function and add it to my little array.

  function DoSomethingCool()

  {

    // code here

  }

  if (!window.mikesCode)
  {
    mikesCode=new Array();  // create mikesCode if it does not exist
  }
  mikesCode[mikesCode.length] = "DoSomethingCool";   //add the function to run

 

Now we have all of the pieces…  (but I’m not finished with this article… I want to find a way to do this without the master page edit)

 

The new “work around!”

To get our code to run at the right time we need to hook into the SharePoint code that loads the calendar items. Once such place is a function named "SP.UI.ApplicationPages.CalendarNotify.$4a".We also need to delay the load of our code, and we can do that with a SharePoint feature named "_spBodyOnLoadFunctionNames.push".

As a result of this change I can now eliminate the need for the master page edit. (That trick is still useful for other kinds of customizations.)

 

 

Color Coded Events in SharePoint 2010 Calendars Step by Step

 

Add a little piece of code to the master page

  1. Open SharePoint Designer 2010 and open your site
     
  2. In the Navigation pane and in the Site Object section click Master Pages
     
  3. Click your master page (most likely “v4.master”) and if asked, check it out
     
  4. In Customization section click “Edit file”
     
  5. Scroll to the bottom of the file and just before the </body> tag add the script below
     
  6. Save the file and then check it in

<script type="text/javascript">
  if (window.mikesCode)
  {
    for (var i=0;i<mikesCode.length;i++)
    {
      _spBodyOnLoadFunctionNames.push(mikesCode[i]);
    }
  }
</script>

 

Setup the Calendar

The steps here are identical to the SP 2007 article.

  1. Create or open a calendar
     
  2. Add a new column named "Color" – most likely type will be "Choice" with choices like "Red, Green, Blue, Black", but this could be a lookup or a single line of text.
    (See here for an HTML color chart: http://www.w3schools.com/html/html_colornames.asp)
     
  3. Add a new column named "CalendarText"
    1. Column Type is Calculated
    2. Equation is:
          ="<font color='" & Color & "'>" & Title & "</font>"
      image
    3. Data type returned = single line of text
       
  4. Modify the existing view, or create a new view such as "Color Calendar"
     
  5. Change the field used for the Month View Title AND Day View Title AND Week View Title to "CalendarText"
    image
     
  6. Save and exit (The HTML for "<FONT" will now be displayed. Some JavaScript will be needed to fix the HTML tags)

Create the JavaScript file

Two Solutions!

1) Add a Content Editor Web Part as we have done in many of the other customizations. The problem with this approach is that SharePoint 2010 will no longer treat this page as a view. When this page is displayed you will see two changes:

· The View dropdown list in the site title area is missing

· The Ribbon will not be displayed when the page is first loaded, but will be displayed when any event item is clicked.

2) Add the custom JavaScript using SharePoint Designer. The page will then display as an ordinary view no odd behavior.

I recommend #2.

 

Option #1: Using the Content Editor Web Part

I usually add a library named Scripts, ScriptFiles or similar for these kinds of files. The steps below will just use the Shared Documents library.

  1. Open Windows Notepad and add the script below
  2. Save the file with a name like “CalendarColorScript.txt”
  3. Upload the text file to your library (Shared Documents in this example)

<script type="text/javascript" language="javascript">

  function CalColor()
  {
    var x = document.getElementsByTagName("div")
    var i=0;
    for (i=0;i<x.length;i++)
    {
      if (x[i].className=="ms-acal-item")
      {
        x[i].innerHTML= x[i].innerHTML.replace(/&lt;/g,'<').replace(/&gt;/g,'>')
      }
    }
  }

  // hook into the SharePoint JS library to call our code
  // whenever the calendar is updated
  function CalHookIntoSPUI()
  {
    var originalCalendarNotify = SP.UI.ApplicationPages.CalendarNotify.$4a;
    //var originalresetSelection = SP.UI.ApplicationPages.CalendarViewBase.prototype.resetSelection;

    SP.UI.ApplicationPages.CalendarNotify.$4a = function()
    {
      originalCalendarNotify();
      CalColor();
    }
  }

  // add to the array of functions to run from the master page code
  if (!window.mikesCode)
  {
    mikesCode=new Array();
  }
  mikesCode[mikesCode.length] = "CalColor";
  mikesCode[mikesCode.length] = "CalHookIntoSPUI";

</script>

 

(updated 9/6/2011)

<script>
// Color coded calendars for SharePoint 2010
// TechTrainingNotes.blogspot.com

// load our function to the delayed load list
_spBodyOnLoadFunctionNames.push('colorCalendarEventLinkIntercept');

// hook into the existing SharePoint calendar load function
function colorCalendarEventLinkIntercept()
{

  if (SP.UI.ApplicationPages.CalendarNotify.$4a)
  {
    var OldCalendarNotify = 

SP.UI.ApplicationPages.CalendarNotify.$4a;
    SP.UI.ApplicationPages.CalendarNotify.$4a = function () 
      {
        OldCalendarNotify();
        colorCalendarEventLinks();
      }
  }
  if (SP.UI.ApplicationPages.CalendarNotify.$4b)
  {
    var OldCalendarNotify = 

SP.UI.ApplicationPages.CalendarNotify.$4b;
    SP.UI.ApplicationPages.CalendarNotify.$4b = function () 
      {
        OldCalendarNotify();
        colorCalendarEventLinks();
      }
  }
  // future service pack change may go here!
  // if (SP.UI.ApplicationPages.CalendarNotify.???)

}

// hide the hyperlinks
function colorCalendarEventLinks()
{

  // find all DIVs
  var divs = document.getElementsByTagName("DIV");
  for (var i=0;i<divs.length;i++)
  {
    // find calendar item DIVs
    if (divs[i].className.toLowerCase()=="ms-acal-item")
    {
divs[i].innerHTML = divs[i].innerHTML.replace(/&lt;/g,'<').replace(/&gt;/g,'>');
    }

    // find "x more items" links and re-remove links on Expand/Contract
    if (divs[i].className.toLowerCase()=="ms-acal-ctrlitem")
    {
      var links = divs[i].getElementsByTagName("A");
      if (links.length==1)
      {
        links[0].href="javascript:colorCalendarEventLinks();void(0);"
      }
    }

  }
}

</script>

 

Add a Content Editor Web Part to hold the JavaScript

  1. Go to the document library where you uploaded the script file, right-click the file name and select Properties
     
  2. Copy the URL to the file and click OK
     
  3. Display your calendar and select the view you modified earlier
     
  4. Click Site Actions and then Edit Page
     
  5. Click “Add a Web Part” and select the Content Editor web part
    image
     
  6. In the new web part’s dropdown menu click “Edit Web Part”
    image
     
  7. Paste in the URL to the script file you copied in step 2
     
  8. Click OK
     
  9. In the Ribbon click “Stop Editing” (in the Page tab)

 

Option #2 Using SharePoint Designer

This is the preferred method as it does not break view related functionality as does adding a web part.

  1. Go to the calendar in the browser and create a new view (maybe "Calendar - No Links")
     
  2. Open your site in SharePoint Designer 2010
     
  3. Click Lists and Libraries in the Site Objects pane
     
  4. Click your calendar list and click the new view
     
  5. In the ribbon click the "Advanced Mode" link (so you can edit the entire page)
     
  6. Search for "</WebPartPages:WebPartZone>" and insert a new line just after this tag
    </WebPartPages:WebPartZone>
      your customization goes here (don't type this J )
    </asp:Content>
     
  7. Add the JavaScript from below
     
  8. Save your changes in SharePoint Designer
     
  9. Go to the calendar, add a new event and select a color, and see if the colors are displayed.

 

Everything (or at least option #1) comes with a price   :-(

The above works! But… you lose one feature. When you add a web part to a view you lose the ability to select a view from the crumb trail. (This is not a problem if you add the code using SharePoint Designer)

Without a web part on the page:

image

 

With a web part on the page:  (this is our “Color Calendar” view)

image

 

Notice that the crumb trail now only has the list name. The view name is missing. When the view name is displayed then you also have the dropdown to select other views

You can still switch views, but you must first click in the calendar to display the ribbon.

Bug?  Probably.  (So use option #2 – add the code with SharePoint Designer)

 

Some thoughts on color options…

Just change the FONT tag to a SPAN tag and use a style. Example:

    ="<span style='background-color:"&Color&"'>"&Title&"</span>"

This works, but the background is for just the width of the text, not the entire table cell.

You will probably want to use colors like lightblue, lightgreen, tomoto (light red would just be Pink!), etc. or make the text white or a light color:

    ="<span style='color:white;background-color:"&Color&"'>"&Title&"</span>"

image

 

Closing Notes

  • Document everything, and add to your disaster recovery plan (otherwise you’ll never remember how you did this,and consider the poor person who inherits this site from you)
     
  • In researching the SP.UI.ApplicationPages.CalendarNotify update I found a blog article by Mark Wilson that also intercepted the SP.UI.ApplicationPages.CalendarViewBase.prototype.resetSelection function. My code seems to work fine without it. But in any case go take a look at Mark’s article to see another approach to color coded calendars.
     
  • This has been tested only with IE 8 and FF 3.5.9 so far.
     
  • Batteries not included, your mileage may vary, please test, test, test…

.

3/12/2010

SharePoint: Strike Out Canceled Events in the Calendar

 

This article is for SharePoint 2007. For SharePoint 2010 you can use the following steps to customize the columns of the calendar, and then use JavaScript from the Color Calendar article.

 

Moving or deleting an event in a calendar is not always the best way to make sure people notice the change. Sometimes it’s better to “cancel” a meeting and then create a new meeting than just moving it in the calendar. The SharePoint calendar list does not have a built in way to do this. So… yet another JavaScript trick!

 

Here’s what we want to do:

image

 

By the way, the following is just a variation of an earlier article on how to color code a calendar. The “big change” is an IF statement to make the formatting conditional. By expanding the calculated column’s formula a bit you can have both color coded and canceled events.

 

The basic steps are:

  • Add a column to the calendar list to indicate that the event has been canceled 
  • Add a calculated column to create the HTML to display the formatted cancelation message. This can be done with HTML or CSS. This example just uses "<s>  text   </s>"
  • Add the new column to the calendar view
  • SharePoint will convert the "<" character into "&lt;" so we need to add a little JavaScript to convert it back. The easiest way to add the JavaScript is with a Content Editor Web Part

Detailed steps:

  • Create or open a calendar
  • Add a new column named "Canceled" (Settings, List Settings) – most likely type will be "Choice" with choices like "No” and “Yes" with a default of “No”
  • Add a new column named "CalendarText"
    1. Column Type is Calculated
    2. Equation is:
      =IF(Canceled="Yes", "<s>" & Title & "</s>", Title )
                   
    3. Data type returned = single line of text
  • If you want to also change the color, then add a FONT or SPAN tag:
    =IF(Canceled="Yes", "<font color=’gray’><s>" & Title & "</s></font>", Title )

  • Modify the existing view, or create a new view such as "Calendar with cancel"
    1. Change the field used for the “Month View Title” AND “Day View Title” AND “Week View Title” to "CalendarText"
                                image_thumb
    2. Save and exit (The HTML for "<s>" will now be displayed. The JavaScript below will be needed to fix the HTML tags)
  • Add a Content Editor web part
    1. Site Actions, Site Settings, Edit Page
    2. Add a Content Editor web part and move it below the calendar web part
    3. Click in the web part click Edit, Modify Shared Web Part
    4. Click Source Editor
  • Paste this JavaScript:

<script type="text/javascript" language="javascript"> 
var x = document.getElementsByTagName("TD") 
var i=0; 
for (i=0;i<x.length;i++) 
{ 
  if (x[i].className.substring(0,6)=="ms-cal") 
  { 
    x[i].innerHTML= x[i].innerHTML.replace(/&lt;/g,'<').replace(/&gt;/g,'>') 
  } 
} 
</script>  

  • Click Save, OK, Exit Edit Mode
  • Add a new calendar item and select a status – canceled events should now be crossed out. 

 

.

11/03/2008

SharePoint: Color Coded Calendars!

Updated!
Note: If you got the Month view working and the Week and Day views did not then make sure that when you customized the view that you changed all three:

           Change the field used for the Month View Title AND Day View Title AND Week View Title to "CalendarText"

 

For the SharePoint 2010 version of this article click here!

To add “strike-out” for canceled events click here!

For color coding other SharePoint lists click here!

Can a calendar display in color?

I have been asked a few times if the SharePoint calendar can display items in color. It turns out this is not too hard to do. The basic steps are:

  • Add a column to the calendar list to pick the color
  • Add a calculated column to create the HTML to display the color. This can be done with HTML or CSS. This example uses "<FONT COLOR=".
  • Add the new column to the view
  • SharePoint will convert the "<" character into "&lt;" so we need to add a little JavaScript to convert it back. The easiest way to add the JavaScript is with a Content Editor Web Part

Sample:

Color coded SharePoint calendar

  1. Create or open a calendar
  2. Add a new column named "Color" (Settings, List Settings) – most likely type will be "Choice" with choices like "Red, Green, Blue, Black", but this could be a lookup or a single line of text.
    (See here for an HTML color chart: http://www.w3schools.com/html/html_colornames.asp)
  3. Add a new column named "CalendarText"
    1. Column Type is Calculated
    2. Equation is:
      ="<font color='" & Color & "'>" & Title & "</font>"
                   image
    3. Data type returned = single line of text
  4. Modify the existing view, or create a new view such as "Color Calendar"
    1. Change the field used for the Month View Title AND Day View Title AND Week View Title to "CalendarText"
                                image
    2. Save and exit (The HTML for "<FONT" will now be displayed. Some JavaScript will be needed to fix the HTML tags)
  5. Add a Content Editor web part
    1. Site Actions, Site Settings, Edit Page
    2. Add a Content Editor web part and move it below the calendar web part
    3. Click in the web part click Edit, Modify Shared Web Part
    4. Click Source Editor
  6. Paste this JavaScript:

    <script type="text/javascript" language="javascript">
    var x = document.getElementsByTagName("TD")
    var i=0;
    for (i=0;i<x.length;i++)
    {
      if (x[i].className.substring(0,6)=="ms-cal")
      {
        x[i].innerHTML= x[i].innerHTML.replace(/&lt;/g,'<').replace(/&gt;/g,'>')
      }
    }
    </script> 

    image

  7. Click Save, OK, Exit Edit Mode
  8. Add a new calendar item and select a color… Color calendars!

 

Some thoughts on changing the background color and not just the text color…

Just change the FONT tag to a SPAN tag and use a style. Example:

="<span style='background-color:"&Color&"'>"&Title&"</span>"

This works, but the background is for just the width of the text, not the entire table cell. The closest I’ve come is to hard code the width:

="<span style='width:120px;background-color:"&Color&"'>"&Title&"</span>"

This looks good in the Month view, but not so good in Week or Day views.

                              image

You will probably want to use colors like lightblue, lightgreen, tomoto (light red would just be Pink!), etc.


.

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.