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 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