Monday 6 February 2012

Gochas When Using SPCalendarView


Please note that there are a few gotchas when using SPCalendarView in your web part class:

1. Your web part class has to inherit from Microsoft.SharePoint.WebPartPages.WebPart
2. EnableV4Rending of SPCalendarView has to be false otherwise the css class specified in BackgroundColorClassName of SPCalendarItem has no effect
3. BackgroundColorClassName of SPCalendarItem is only applicable if the IsAllDayEvent is set to true
4. There is a bug with SPCalendarView control when EnableV4Rending is set to true, SPCalendarItem stays when switching month, this can be fixed by applying December 2011 SharePoint Foundation CU



using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
 
namespace WebPartTest.MyCalendarWebPart
{
    [ToolboxItemAttribute(false)]
    public class MyCalendarWebPart :  Microsoft.SharePoint.WebPartPages.WebPart
    {
        private SPCalendarView calendar;
 
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
 
            calendar = new SPCalendarView();
            calendar.EnableV4Rendering = false;
            calendar.SelectedDate = GetSelectedDate();
            calendar.EnableViewState = true;
            calendar.ViewType = GetViewType();
            calendar.DataSource = GetCalendarItems();
            calendar.DataBind();
            Controls.Add(calendar);
        }
 
        private SPCalendarItemCollection GetCalendarItems()
        {
            SPCalendarItemCollection items = new SPCalendarItemCollection();
 
            items.Add(new SPCalendarItem(){
                StartDate = DateTime.Now,
                EndDate = DateTime.Now.AddHours(5),
                hasEndDate = true,
                IsAllDayEvent = true,
                Title = "Lunch",
                Location = "KFC",
                CalendarType = Convert.ToInt32(SPCalendarType.Gregorian),
                BackgroundColorClassName = "LunchClass",
                DisplayFormUrl = "www.google.com"
            });
 
            items.Add(new SPCalendarItem()
            {
                StartDate = DateTime.Now.AddDays(1),
                EndDate = DateTime.Now.AddDays(2),
                hasEndDate = true,
                IsAllDayEvent = true,
                Title = "Vacation",
                Location = "Cuba",
                CalendarType = Convert.ToInt32(SPCalendarType.Gregorian),
                BackgroundColorClassName = "VacationClass",
                DisplayFormUrl = "www.microsoft.com"
            });
 
            return items;
        }
 
        private string GetViewType() 
        { 
            string viewType = string.Empty;
            string calendarPeriod = Page.Request.QueryString["CalendarPeriod"];
            if (calendarPeriod != null) 
            {
                switch (calendarPeriod.ToString().ToLower()) 
                { 
                    case "day":                     
                        viewType = "day"; 
                        break; 
                    case "month":                     
                        viewType = "month"; 
                        break; 
                    case "timeline":                     
                        viewType = "timeline"; 
                        break; 
                    default:                     
                        viewType = "week"; 
                        break; }
            }
            else { viewType = "week"; } 
            
            return viewType;
        } 
 
        private string GetSelectedDate()
        {
            string selectedDate = Page.Request.QueryString["date"];
        
            return (selectedDate == null)?String.Empty:selectedDate; 
        }
    }
}

2 comments:

  1. I am running into the issue with when EnableV4Rendering = false, everything is good but if take that code out my items/events are seen on all months when a user clicks a previous or next button. Does your code above provide a fix for this issue?

    Thanks,
    Patrick

    ReplyDelete
    Replies
    1. As I mentioned in point #4, this is a bug and can be fixed by applying SharePoint Foundation December 2011 Cumulative Update. You can follow the link below:
      http://support.microsoft.com/kb/2597058

      Delete