Custom Calendar Webpart in Sharepoint 2010

Here is a code snippet for Webpart which will display a calendar using SPCalendarView control; inheriting from Microsoft.SharePoint.WebPartPages.WebPart namespace.

using System;

using System.Runtime.InteropServices;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Serialization;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.WebPartPages;

namespace SampleWebpart1


public class SampleWebpart1 : Microsoft.SharePoint.WebPartPages.WebPart //System.Web.UI.WebControls.WebParts.WebPart

public SampleWebpart1() { }

protected override void CreateChildControls()


SharePointCalendar calendar = new SharePointCalendar();


public class SharePointCalendar : Control


private SPCalendarView _view;
/// Create the SharePoint calendar. Uses the SharePoint SPCalendarView object.

protected override void CreateChildControls()



_view = new SPCalendarView();

_view.EnableViewState = true;

_view.Width = Unit.Percentage(100);

_view.DataSource = GetCalendarItems();




private SPCalendarItemCollection GetCalendarItems()

// Create a new collection for the calendar items

// This is an item with a start and end date.

SPCalendarItemCollection items = new SPCalendarItemCollection();

// Add the first dummy item

SPCalendarItem item = new SPCalendarItem();

item.StartDate = DateTime.Now;

item.EndDate = DateTime.Now.AddHours(1);

item.hasEndDate = true;

item.Title = "First calendar item";

item.DisplayFormUrl = "/News";

item.Location = "USA";

item.Description = "This is the first test item in the calendar rollup";

item.IsAllDayEvent = false;

item.IsRecurrence = false;

item.CalendarType = Convert.ToInt32(SPCalendarType.Gregorian);


// Add the second item. This is an all day event.

SPCalendarItem item2 = new SPCalendarItem();

item2.StartDate = DateTime.Now.AddDays(-1);

item.hasEndDate = true;

item2.Title = "Second calendar item";

item2.DisplayFormUrl = "/News";

item2.Location = "India";

item2.Description = "This is the second test item in the calendar rollup";

item2.IsAllDayEvent = true;

item2.IsRecurrence = false;

item2.CalendarType = Convert.ToInt32(SPCalendarType.Gregorian);


// return the collection

return items;




Post a Comment


This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at .

Copyright (c) 2010 @ All rights are reserved.Do Not Copy.

@ Learning