Tuesday, February 15, 2011

Redirect calendar view Web Parts to any date and any view

This only works for WSS 3.0 and MOSS 2007.  2010 works completely different.

There was an interesting email I received in my inbox the other day.  It came from @KerriAbraham and said: How can I display next week on a weekly calendar view?  I must be missing something obvious.

With an email like this, I always refer that person to a forum.  If it’s worth asking, then it’s worth sharing…  Here’s a link to the post: http://endusersharepoint.com/STP/viewtopic.php?f=10&t=2835

Looking at an OOTB calendar view, I noticed when changing the dates and views from day, week, and month; the URL updated with new query strings.  These query strings are CalendarPeriod & CalendarDate.

CalendarPeriod

This parameter (query string) accepts 3 different values.  Each of these are valid and produce different views for the calendar.

  1. CalendarPeriod=day
  2. CalendarPeriod=week
  3. CalendarPeriod=month

When this parameter is set, the calendar will automatically update to day, week, or month.

CalendarDate

This parameter works a little different, but still follows a consistent pattern.  This parameter accepts a date. (I’m captain obvious, I know…)  The format of the date should be:

M/DD/YYYY

Since we can’t literally put “/” in a parameter and expect it to work, we need to encode those values.  With that in mind “/” turns into “%2F”.  So our parameter’s value really looks like this:

2%2F15%2F2011

URL with parameters set

Now let’s look at what the URL actually will look like if we wanted to do a view based on the week and the day of Feb. 20, 2011.

CalendarQueryString

You are probably thinking right now…  I knew all of this stuff, did I really just waste 2 minutes reading this post?  Absolutely not… You need to know all of this stuff and a reiteration of the basics will help you understand the script below. 

RedirectCalendar.js

This script is based off of Christophe Humbert’s redirect script in the SharePoint User’s Toolkit.  There are lots of excellent scripts stored here that will change how you use SharePoint.  Also, this post is a great example of it, do not take solutions you find on the internet for face value.  With that in mind, let’s get on with it…

URL="http://URL/Calendar/WebPartPage.aspx",redirectDay=5,popup="You will be redirected automatically.";
var d=new Date();
var currDay = d.getDay();
var currMonth = d.getMonth();
var currYear = d.getFullYear();
currMonth++;
// Calculate day to redirect to
var redirectToDay = d.getDate() + 7;

/*
Sets query string for redirect.
To redirect to Month view; Change CalendarPeriod to month.
*** ?CalendarPeriod=month ***
To redirect to Day view; Change CalendarPeriod to equal
*** ?CalendarPeriod=day ***
*/
qString = "?CalendarPeriod=week&CalendarDate="+currMonth+"%2F"+redirectToDay+"%2F"+ currYear;

function Redirect() {
// Look for existing query strings. If found, do nothing.
if (window.location.href.indexOf("?CalendarPeriod") >= 0 || window.location.href.indexOf("&CalendarDate") >= 0) {
return false;
} else {
if ((!(currDay == redirectDay))) {
return false;
} else {
if (popup) { alert(popup); }
window.location.href=URL+qString;
}
}
}
Redirect();


This script allows you to set your variables without much trouble.  The variables that you should be concerned with are:




  • URL


  • redirectDay


  • popup


  • redirectToDay



The URL needs to be set to the page where your calendar view exists.



CalendarCurrentURL



The redirectDay is used for what day you’d like for the redirect to occur.  Sunday = 0; Saturday = 6.  This script is locked and loaded for Friday.



popup is used for alerting the user redirection is about to happen.  If you don’t like this, then set popup=””.  That will make popup equal false under the hood and not bug you.



The last variable is redirectToDay.  If you look at the code, you’ll notice this variable is being set based off of the current date.  Kerri wanted to add 7 days to the redirect on Friday.  That’s why you see:



var redirectToDay = d.getDate() + 7;


Feel free to add however many days you’d like to this date.  After you’ve changed all of your variables, you’ll notice when you visit this page on the day you’ve configured, the redirect will automatically happen.  Hope you like…

2 comments:

Christophe said...

Funny to read the "I must be missing something obvious". Because I am working with SP 2010, and having exactly the same thought...

@iOnline247 said...

I'm guessing in the rush to make everything Ajax, they've stripped out some key capabilities. I'd love to be able to do this with a 2010 calendar. Not sure what I'd have to do yet though...