標籤:c style class blog code java
[from] http://www.cnblogs.com/love007/archive/2013/06/26/3156852.html
採用類比帳號的方式讀取行事曆資訊,注意下日曆的內容讀取(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;
}
調用後出現如下錯誤:
所以只能用Regex擷取常值內容。
附帶正確代碼:
#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