Search This Blog

Saturday, April 21, 2012

SPCALENDARVIEW

private SPCalendarItemCollection GetCalendarItems(DataTable results)
{
SPCalendarItemCollection items = new SPCalendarItemCollection();
using (SPWeb web = SPContext.Current.Web)
{
foreach (DataRow row in results.Rows)
{
SPCalendarItem item = new SPCalendarItem();

item.Title = row["Location"] as string +" "+ row["Title"] as string + "br" + Convert.ToDateTime(row["EventDate"]).ToShortTimeString() + "\n" + Convert.ToDateTime(row["EndDate"]).ToShortTimeString();
item.StartDate = (System.DateTime)row["EventDate"];
item.EndDate = (System.DateTime)row["EndDate"];
item.hasEndDate = true;
item.IsAllDayEvent = true;
item.CalendarType = Convert.ToInt32(SPCalendarType.Gregorian);

items.Add(item);
}
}

return items;
}

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

MyCalendar.DataSource = GetCalendarItems();

DataBind();

}

}

private SPCalendarItemCollection GetCalendarItems()

{

SPCalendarItemCollection calItems = new SPCalendarItemCollection();

using (SPSite site = new SPSite("your web url"))

{

using (SPWeb web = site.OpenWeb())

{

SPList list = web.GetList("Lists/IWCalendar");

foreach (SPListItem it in list.Items)

{

SPCalendarItem calItem = new SPCalendarItem();

calItem.StartDate = DateTime.Parse(it["Start Time"].ToString());

calItem.EndDate = DateTime.Parse(it["End Time"].ToString());

calItem.hasEndDate = true;

calItem.Title = it.Title;

///else properties

calItems.Add(it);

}

}

}

return calItems;

}


protected void EventsCalendar2_PreRender(object sender, EventArgs e)
{

SPWeb oWebsite = SPContext.Current.Web;
SPList oList = oWebsite.Lists["DateEvent"];
SPListItemCollection collListItems = oList.Items;
EventsCalendar2.DataSource = GetCalendarItems(oList.Items.GetDataTable());
EventsCalendar2.DataBind();
}

No comments:

Post a Comment