Creating a Global Calendar Webpart using SPCalendarView


In order to make such a custom Calendar Webpart that will display events from all Calendars on the site, Recurring and non-Recurring Events need to be treated differently. We need to expand the Recurring series using <DaterangesOverLap> in SPQuery.Query. Also , need to initialise SPCalendarItem's ItemId property with either "Edit Menu Table End" or "Select" Field's value to make sure the SPCalendarItem.DisplayFormUrl  is correct.


The code demonstrating the usage is below
The SPCalendarItemCollection returned can be assigned to SPCalendarView Datasource as in :


SPCalendarView calendar = null;

calendar = new SPCalendarView();

calendar.Visible = true;

calendar.EnableViewState = true;

calendar.ViewType = GetCalendarType(Page.Request["CalendarPeriod"]);

calendar.DataSource = getDataFromCalendar(somesite, somecalendar);

calendar.DataBind();

And then rendered in a custom Webpart.


Here's the code that can be used to get all events after specifying SPSite url and Events List Title as parameters :


<Sample-Code>

  private SPCalendarItemCollection getDataFromCalendar(String str_site_collection, String str_calendar)
{
string temp = "";
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite sitex = new SPSite(str_site_collection))
{
SPWeb oWeb = sitex.OpenWeb();
SPList oList = oWeb.Lists[str_calendar];
SPCalendarItem citem = null;
SPListItem citemx = null;

foreach (SPListItem item in oList.Items)
{
if (!(bool.Parse(item["Recurrence"].ToString())))
{
citem = new SPCalendarItem();
citem.ItemID = item["ID"].ToString();
try
{
citem.StartDate = DateTime.Parse(item["EventDate"].ToString());

}
catch { }

try
{
citem.EndDate = DateTime.Parse(item["EndDate"].ToString());
}
catch { }
temp = item["Title"] as string;
temp += " " + item["RecurrenceData"];
citem.Title = item["Title"] as string;
temp = oList.ParentWebUrl + "/" + oList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url;
citem.DisplayFormUrl = temp.Replace("//", "/");
citem.Location = item["Location"] as string;
citem.Description = item["Description"] as string;

if (bool.Parse(item["Recurrence"].ToString()))
{
citem.IsRecurrence = true;
}
else
{
citem.IsRecurrence = false;
}
citem.CalendarType = Convert.ToInt32(SPCalendarType.Gregorian);
collection.Add(citem);
}
}
SPQuery queryRecurrence = new SPQuery();
queryRecurrence.ExpandRecurrence = true;
queryRecurrence.Query = "<Where>" +
"<And>" +
"<Eq>" +
"<FieldRef Name='fRecurrence' />" +
"<Value Type='Recurrence'>1</Value>" +
"</Eq>" +
"<DateRangesOverlap>" +
"<FieldRef Name='EventDate' />" +
"<FieldRef Name='EndDate' />" +
"<FieldRef Name='RecurrenceID' />" +
"<Value Type='DateTime'>" +
"<Month />" +
"</Value>" +
"</DateRangesOverlap>" +
"</And>" +
"</Where>" +
"<ViewFields>" +
"<FieldRef Name='Title' />" +
"<FieldRef Name='EventDate' />" +
"FieldRef Name='EndDate' />" +
"<FieldRef Name='fRecurrence' />" +
"<FieldRef Name='Absentee' />" +
"</ViewFields>";

// Look forward from the beginning of the current month
queryRecurrence.CalendarDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
// Returns all items (including recurrence instances) that
// would appear in the calendar view for the current month
SPListItemCollection calendarItems = oList.GetItems(queryRecurrence);
foreach (SPListItem listItem in oList.GetItems(queryRecurrence))
{
SPCalendarItem calItem = new SPCalendarItem();
calItem.ItemID = listItem["Edit Menu Table End"].ToString();
calItem.Title = listItem["Title"].ToString();
calItem.CalendarType = Convert.ToInt32(SPCalendarType.Gregorian);
calItem.StartDate = (DateTime)listItem["Start Time"];
if (listItem["End Time"] != null)
{
calItem.hasEndDate = true;
calItem.EndDate = (DateTime)listItem["End Time"];
}
else
calItem.hasEndDate = false;

if (listItem["Description"] != null)
calItem.Description = listItem["Description"].ToString();

if (listItem["Location"] != null)
calItem.Location = listItem["Location"].ToString();
temp = "";

temp = oList.ParentWebUrl + "/" + oList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url;
// temp += "?ID=" + listItem["Edit Menu Table End"];
calItem.DisplayFormUrl = temp.Replace("//", "/");
collection.Add(calItem);
}
}
}
);
return collection;
}

</Sample-Code>
I will post the full code once I have tested it.
 


Comments (5)
  1. avi says:

    I would like to use your code. If it is not against your ethics can you please share your code on avi0812652@yahoo.com

    Avi

  2. theSpmarket says:

    We got a similar webpart for global calendar at the below link

    http://www.thesharepointmarket.com/…/global-calendar

  3. SP_learner says:

    Can you post your full code or webpart if possible?  Thanks.

  4. Kumaravelu says:

    This is looks fine. if possible, possible share ur code to kumaravelu999@gmail.com

  5. Johnny B says:

    This little recurring event thing has been driving me nuts for some time now, thanks a million for explaining it and providing the code for the recurrence bit.

Comments are closed.

Skip to main content