EWS API 2.0读取日历信息-读取内容注意事项
采用模拟账号的方式读取日历信息,注意下日历的内容读取(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 BodyPropertySet 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 BodyPropertySet 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();//urlinfo.Url = item.WebClientReadFormQueryString;//加入如下,表示读取内容,否则会提示如下://HTML如果不为空if (item.Body != null){//html格式的内容MessageBody body = item.Body;//读取文本info.BodyHtml = body.Text;}////读取idif (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;}//returnreturn CalendarInfoList;}#endregion


浙公网安备 33010602011771号