Sample: Using Calendar Views with EWS.
When working with Exchange Web Services (EWS) to do calendaring operations, you will likely run into the need to define calendar views. A calendar view is a restriction/filer on the timespan of appointments and meetings you want to see in the calendar. To a beginner, how defining a calendar view properly can be a little bit confusing. So, I put together some sample code I’ve used prior and am providing it here in case your looking for such samples.
Here is a sample of the calling code:
CalendarViewType cvtView = GetCalendarViewForDay(2007,1, 6);
txtDayView.Text = RequestAndDisplayCalendarView(_esb, cvtView);
Here is the implemented code:
// -----------------------------------------------------------------
// GetCalendarViewForToday
// Returns a calendar view for today.
// -----------------------------------------------------------------
public static CalendarViewType GetCalendarViewForToday()
{
return GetCalendarViewForDay(
DateTime.Now.Year,
DateTime.Now.Month,
DateTime.Now.Day
);
}
// -----------------------------------------------------------------
// GetCalendarViewForDay
// Returns a calendar view for a given day.
// -----------------------------------------------------------------
public static CalendarViewType GetCalendarViewForDay(int iYear, int iMonth, int iDay)
{
DateTime dtToday12AM = new DateTime(iYear, iMonth, iDay, 0, 0, 0, DateTimeKind.Local);
CalendarViewType calendarView = new CalendarViewType();
calendarView.StartDate = dtToday12AM;
calendarView.EndDate = calendarView.StartDate.AddDays(1);
calendarView.MaxEntriesReturned = 99;
calendarView.MaxEntriesReturnedSpecified = true;
return calendarView;
}
// -----------------------------------------------------------------
// GetCalendarViewForWeek
// Returns a calendar view for the week of a given date.
// -----------------------------------------------------------------
public static CalendarViewType GetCalendarViewForWeek(int iYear, int iMonth, int iDay)
{
// create a date time instance which represents the day at 12:00:00am
DateTime workDateTime = new DateTime(iYear, iMonth, iDay, 0, 0, 0, DateTimeKind.Local);
DateTime FirstDOW = GetFirstDOW(workDateTime);
DateTime LastDOW = GetLastDOW(workDateTime);
CalendarViewType calendarView = new CalendarViewType();
calendarView.StartDate = FirstDOW;
calendarView.EndDate = LastDOW;
calendarView.MaxEntriesReturned = 99;
// Don’t forget to set the specified flag
calendarView.MaxEntriesReturnedSpecified = true;
return calendarView;
}
// -----------------------------------------------------------------------
// GetFirstDOW
// Returns a the first day of the week for a given date.
// In this routine, Sunday is considered to be the first day of the
// However, the day considered to be the first day of the week may vary depending
// upon culture. Cultural specific coding is not covered in this sample.
// -----------------------------------------------------------------------
public static DateTime GetFirstDOW(DateTime dtDate)
{
DateTime FirstDayofWeek = dtDate;
//CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek
switch (dtDate.DayOfWeek)
{
case DayOfWeek.Sunday:
FirstDayofWeek = dtDate;
break;
case DayOfWeek.Monday:
FirstDayofWeek = dtDate.AddDays(-1);
break;
case DayOfWeek.Tuesday:
FirstDayofWeek = dtDate.AddDays(-2);
break;
case DayOfWeek.Wednesday:
FirstDayofWeek = dtDate.AddDays(-3);
break;
case DayOfWeek.Thursday:
FirstDayofWeek = dtDate.AddDays(-4);
break;
case DayOfWeek.Friday:
FirstDayofWeek = dtDate.AddDays(-5);
break;
case DayOfWeek.Saturday:
FirstDayofWeek = dtDate.AddDays(-6);
break;
}
return FirstDayofWeek;
}
// -----------------------------------------------------------------------
// GetLastDOW
// Returns a the last day of the week for a given date.
// In this routine, Sunday is considered to be the last day of the
// However, the day considered to be the last day of the week may vary depending
// upon culture. Cultural specific coding is not covered in this sample.
// -----------------------------------------------------------------------
public static DateTime GetLastDOW(DateTime dtDate)
{
DateTime LastDayofWeek = dtDate;
//CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek
switch (dtDate.DayOfWeek)
{
case DayOfWeek.Sunday:
LastDayofWeek = dtDate.AddDays(6);
break;
case DayOfWeek.Monday:
LastDayofWeek = dtDate.AddDays(5);
break;
case DayOfWeek.Tuesday:
LastDayofWeek = dtDate.AddDays(4);
break;
case DayOfWeek.Wednesday:
LastDayofWeek = dtDate.AddDays(3);
break;
case DayOfWeek.Thursday:
LastDayofWeek = dtDate.AddDays(2);
break;
case DayOfWeek.Friday:
LastDayofWeek = dtDate.AddDays(1);
break;
case DayOfWeek.Saturday:
LastDayofWeek = dtDate;
break;
}
return LastDayofWeek;
}
// -----------------------------------------------------------------------
// GetCalendarViewForMonth
// Returns a calendar view for a given year/month
// -----------------------------------------------------------------------
public static CalendarViewType GetCalendarViewForMonth(int iYear, int iMonth)
{
DateTime dtLastDayOfMonth = new DateTime(
iYear,
iMonth,
DateTime.DaysInMonth(iYear, iMonth)
);
DateTime dtStart = new DateTime(
iYear, iMonth, 1,
0, 0, 0,
DateTimeKind.Local
);
CalendarViewType calendarView = new CalendarViewType();
calendarView.StartDate = dtStart;
calendarView.EndDate = dtLastDayOfMonth;
calendarView.MaxEntriesReturned = 100;
calendarView.MaxEntriesReturnedSpecified = true;
return calendarView;
}
//----------------------------------------------------------------------
// GetCalendarItems
// Returns a list of calendar items as text based on a calendar view.
//----------------------------------------------------------------------
public static string GetCalendarItems(
ExchangeServiceBinding binding,
CalendarViewType calendarView)
{
string sLine = "";
string sLines = "";
FindItemType findItemRequest = new FindItemType();
findItemRequest.ParentFolderIds = new DistinguishedFolderIdType[] { new DistinguishedFolderIdType() };
((DistinguishedFolderIdType)findItemRequest.ParentFolderIds[0]).Id =
DistinguishedFolderIdNameType.calendar;
findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
ItemResponseShapeType itemShapeDefinition = new ItemResponseShapeType();
itemShapeDefinition.BaseShape = DefaultShapeNamesType.AllProperties;
findItemRequest.Item = calendarView;
findItemRequest.ItemShape = itemShapeDefinition;
// Do the EWS Call...
FindItemResponseType findItemResponse = binding.FindItem(findItemRequest);
// Check for errors?
if (findItemResponse.ResponseMessages.Items[0].ResponseClass !=
ResponseClassType.Success)
{
throw new Exception(String.Format( "Error:\r\n{0}\r\n{1}",
findItemResponse.ResponseMessages.Items[0].ResponseCode,
findItemResponse.ResponseMessages.Items[0].MessageText));
}
FindItemResponseMessageType findItemResponseMessage =
(FindItemResponseMessageType)findItemResponse.
ResponseMessages.Items[0];
ArrayOfRealItemsType findItemResponseItems =
(ArrayOfRealItemsType)findItemResponseMessage.RootFolder.Item;
sLine = string.Format(
"There are {0} appointments between \r\n\t{1} on {2} and\r\n" +
"\t{3} on {4}\r\n------------------------------\r\n",
findItemResponseMessage.RootFolder.TotalItemsInView,
calendarView.StartDate.ToLongTimeString(),
calendarView.StartDate.ToLongDateString(),
calendarView.EndDate.ToLongTimeString(),
calendarView.EndDate.ToLongDateString());
Debug.WriteLine(sLine);
sLines = sLine;
for (int x = 0;
x < findItemResponseMessage.RootFolder.TotalItemsInView;
x++)
{
CalendarItemType currentCalendarItem =
(CalendarItemType)findItemResponseItems.Items[x];
Debug.WriteLine(currentCalendarItem.Subject);
// Exchange stores all calendaring data in UTC time. So, convert to local
// time so the person looking at the dates dont have to do the time math.
sLine = " Starts at: "+
currentCalendarItem.Start.ToLocalTime().ToShortTimeString() +
" on " + currentCalendarItem.Start.ToLocalTime().DayOfWeek +
" (" + currentCalendarItem.Start.ToString() + ")";
Debug.WriteLine(sLine);
sLines += sLine + "\r\n";
sLine = " Ends at: " +
currentCalendarItem.End.ToLocalTime().ToShortTimeString() +
" on " + currentCalendarItem.End.ToLocalTime().DayOfWeek +
" (" + currentCalendarItem.End.ToString() + ")";
Debug.WriteLine(sLine);
sLines += sLine + "\r\n";
Debug.WriteLine(" RecurrenceId: " + currentCalendarItem.RecurrenceId);
Debug.WriteLine(" Id: " + currentCalendarItem.ItemId.Id);
Debug.WriteLine(" ChangeKey: " + currentCalendarItem.ItemId.ChangeKey);
Debug.WriteLine(" Organizer Name: " + currentCalendarItem.Organizer.Item.Name);
Debug.WriteLine(" IsRecurring: " + currentCalendarItem.IsRecurring.ToString());
Debug.WriteLine(" IsMeeting: " + currentCalendarItem.IsMeeting.ToString());
Debug.WriteLine(" OriginalStart: " + currentCalendarItem.OriginalStart.ToString());
Debug.WriteLine(" HasAttachments: " + currentCalendarItem.HasAttachments.ToString());
Debug.WriteLine(" Attachments: ");
sLines += " RecurrenceId: " + currentCalendarItem.RecurrenceId+ "\r\n";
sLines += " Id: " + currentCalendarItem.ItemId.Id+ "\r\n";
sLines += " ChangeKey: " + currentCalendarItem.ItemId.ChangeKey+ "\r\n";
sLines += " Organizer Name: " + currentCalendarItem.Organizer.Item.Name+ "\r\n";
sLines += " IsRecurring: " + currentCalendarItem.IsRecurring.ToString()+ "\r\n";
sLines += " IsMeeting: " + currentCalendarItem.IsMeeting.ToString()+ "\r\n";
sLines += " OriginalStart: " + currentCalendarItem.OriginalStart.ToString()+ "\r\n";
sLines += " HasAttachments: " + currentCalendarItem.HasAttachments.ToString()+ "\r\n";
sLines += " Attachments: "+ "\r\n";
// Note: You should not rely on the HasAttachments.
// Please refer to:
// Example: Returning a list of attachments using EWS
Debug.WriteLine(" xxxxxx xxxxxx xxxxxx xxxxxx xxxxxx");
Debug.WriteLine("");
sLines += " \r\n xxxxxx xxxxxx xxxxxx xxxxxx xxxxxx" + "\r\n";
sLines += "" + "\r\n";
}
return sLines;
}
Comments
Anonymous
January 05, 2009
PingBack from http://www.codedstyle.com/sample-using-calendar-views-with-ews/Anonymous
January 09, 2009
Here is a helpful sample of using EWS to get a list of calendar items and displaying them in a ListView.Anonymous
January 21, 2009
I've put together a list of articles which cover common questions on Exchange Web Services (EWS). TheseAnonymous
January 12, 2010
Thank you so much for the article It works fine for me to retrieve my calandar But I am facing a task of loading a shared/public calendar that placed in a public folder Can you help for what we should make in the GetCalendarItems function Thanks a lot