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
{
[Guid("cf5f3fd5-1776-4c47-9587-4f6fe4f3d645")]
public class SampleWebpart1 : Microsoft.SharePoint.WebPartPages.WebPart //System.Web.UI.WebControls.WebParts.WebPart
{
public SampleWebpart1() { }
protected override void CreateChildControls()
{
base.CreateChildControls();
SharePointCalendar calendar = new SharePointCalendar();
Controls.Add(calendar);
}
}
public class SharePointCalendar : Control
{
private SPCalendarView _view;
///
/// Create the SharePoint calendar. Uses the SharePoint SPCalendarView object.
///
protected override void CreateChildControls()
{
base.CreateChildControls();
_view = new SPCalendarView();
_view.EnableViewState = true;
_view.Width = Unit.Percentage(100);
_view.DataSource = GetCalendarItems();
DataBind();
Controls.Add(_view);
}
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);
items.Add(item);
// 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);
items.Add(item2);
// return the collection
return items;
}
}
}
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
{
[Guid("cf5f3fd5-1776-4c47-9587-4f6fe4f3d645")]
public class SampleWebpart1 : Microsoft.SharePoint.WebPartPages.WebPart //System.Web.UI.WebControls.WebParts.WebPart
{
public SampleWebpart1() { }
protected override void CreateChildControls()
{
base.CreateChildControls();
SharePointCalendar calendar = new SharePointCalendar();
Controls.Add(calendar);
}
}
public class SharePointCalendar : Control
{
private SPCalendarView _view;
///
/// Create the SharePoint calendar. Uses the SharePoint SPCalendarView object.
///
protected override void CreateChildControls()
{
base.CreateChildControls();
_view = new SPCalendarView();
_view.EnableViewState = true;
_view.Width = Unit.Percentage(100);
_view.DataSource = GetCalendarItems();
DataBind();
Controls.Add(_view);
}
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);
items.Add(item);
// 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);
items.Add(item2);
// return the collection
return items;
}
}
}
0 comments:
Post a Comment