采用模擬賬號的方式讀取日歷信息,注意下日歷的內容讀取(Body)讀取。代碼如下:(采用 EWS API 2.0版本)
1、讀取內容前必須設置如下屬性:否則會提示:You must load or assign this property before you can read its value Body
如下圖:
//*************************以為設置為讀取內容,否則會提示:You must load or assign this property before you can read its value Body
PropertySet detailedPropertySet = new PropertySet(BasePropertySet.FirstClassProperties, AppointmentSchema.Recurrence);
service.LoadPropertiesForItems(from Item item in findResults select item, detailedPropertySet);
//******************************
設置后正常。
2、如果想讀取內容的純文本,目前Exchange server2010內的版本支持讀取帶HTML的內容。調用代碼如下:
//如果文本不為空if (item.TextBody != null)
{TextBody txtBody = item.TextBody;//info.BodyText = txtBody.Text;}
調用后出現如下錯誤:
所以只能用正則表達式獲取文本內容。
附帶正確代碼:
#region//讀入日歷信息
/// <summary>
/// 讀入日歷信息
/// </summary>
/// <param name="config">配置參數</param>
/// <param name="searchdtStart">開始時間</param>
/// <param name="searchdtEnd">結束時間</param>
/// <returns>返回列表</returns>
private static List<CalendarInfo> GetCalendarList(EwsConfig config,DateTime searchdtStart,DateTime searchdtEnd){//返回值
List<CalendarInfo> CalendarInfoList = new List<CalendarInfo>();
try
{//讀取未讀郵件
CalendarFolder calendarfolder = (CalendarFolder)Folder.Bind(service, WellKnownFolderName.Calendar);//如果不為空
if (calendarfolder != null){//檢索開始時間和結束時間
CalendarView calendarView = new CalendarView(searchdtStart, searchdtEnd);
//檢索數據
FindItemsResults<Appointment> findResults = calendarfolder.FindAppointments(calendarView);//*************************以為設置為讀取內容,否則會提示:You must load or assign this property before you can read its value Body
PropertySet detailedPropertySet = new PropertySet(BasePropertySet.FirstClassProperties, AppointmentSchema.Recurrence);
service.LoadPropertiesForItems(from Item item in findResults select item, detailedPropertySet);
//******************************
//返回
foreach (Appointment item in findResults.Items){//實體類
CalendarInfo info = new CalendarInfo();
//主題
info.Identity = item.ICalUid;//來源
info.Source = "Exchange2010";
//主題
info.Subject = item.Subject;//地區
info.Location = item.Location;//開始時間
info.StartTime = item.Start.ToLocalTime();//結束時間
info.EndTime = item.End.ToLocalTime();//url
info.Url = item.WebClientReadFormQueryString;//加入如下,表示讀取內容,否則會提示如下:
//HTML如果不為空
if (item.Body != null){//html格式的內容
MessageBody body = item.Body;//讀取文本
info.BodyHtml = body.Text;}//
//讀取id
if (item.Id != null){info.ItemIdType = new CalendarInfo.CalendarItemIdType { Id = item.Id.UniqueId, ChangeKey = item.Id.ChangeKey };
}//加入到集合中去
CalendarInfoList.Add(info);}}}catch (Microsoft.Exchange.WebServices.Data.ServiceResponseException ex)
{throw ex;
}//return
return CalendarInfoList;
}#endregion