Search This Blog

Saturday, May 29, 2010

Linking ASP.Net Calendar to SharePoint Calendar - Day Click

Recently I came across a requirement where asp.net Calendar control was shown in a Page Layout and on day click of this asp.net calendar control, the user should be redirected to the SharePoint out of the box calendar showing that day.

ScreenShot: ASP.Net calendar control.


Solution:
The asp.net calendar provides a OnSelectionChanged event. Create a new class that inherits from the asp.net calendar class and override OnSelectionChanged.  Find the date selected (clicked) on the calendar
and redirect to the Calendar day.

The good thing about the out of the box SharePoint Calendar is that it provides a simple mechanism to display a month or day view by passing parameters thru URL i.e.
CalendarDate  and CalendarPeriod

  1. CalendarPeriod can be month or day. If this is month - month view is shown and if this is day - that specific day is shown on the SharePoint Calendar page
  2. CalendarDate is date in the format mm/dd/yyyy

Here is the complete function for reference:

/// <summary>


/// Captures the Date click event and redirects to Calendar Day page

/// </summary>

protected override void OnSelectionChanged()

{



string url = string.Empty;



if (SPContext.Current != null)

{
/* Find the current site URL and prefix so that this works in all environment i.e. Dev, QA, Staging, Prod */
url = SPUrlUtility.CombineUrl(SPContext.Current.Site.Url, "Lists/My Calendar Name Here/calendar.aspx?CalendarDate={0}&CalendarPeriod={1}");

}
/* Since date is mm/dd/yyyy format , encode the date so that front slash is encoded. Also replace the value of  {0} i.e. First  and {1} i.e. Second parameter defined in the url string above. */
url = string.Format(url, SPHttpUtility.UrlKeyValueEncode(this.SelectedDate.ToShortDateString()), "day");

// Redirect the page to the Calendar day view
Page.Response.Redirect(url);



}

I will post a sequel to this i.e. how to link the title of the ASP.Net Calendar to SharePoint Calendar. Coming soon...

Please post your comments if you are reading this...

-- Mohan