如何使用Exchange Web Service获取日历(包含循环会议)
本文介绍如何使用EWS代理类调用Exchange的日历
(1)首先生成代理类
ExchangeServiceBinding _ExchangeBinding = new ExchangeServiceBinding(); _ExchangeBinding.Credentials = new NetworkCredential(username, password, domain); _ExchangeBinding.Url = Helper.GetWebconfig("exchangewebservice");
(2)定义请求的属性
请注意:在定义请求属性时,一定要定义CalendarViewType属性,默认的日历返回是不含循环日历的,
如果需要返回循环会议请求,就需要等译CalendarView
FindItemType findItemRequest = new FindItemType(); ItemResponseShapeType itemProperties = new ItemResponseShapeType(); itemProperties.BaseShape = DefaultShapeNamesType.AllProperties; findItemRequest.ItemShape = itemProperties; //我们查找的是日历 DistinguishedFolderIdType[] folderIDs = { new DistinguishedFolderIdType { Id = DistinguishedFolderIdNameType.calendar } };
findItemRequest.ParentFolderIds = folderIDs; //增加搜索的日历范围,红色代码要饭服务器返回循环会议 CalendarViewType cldview = new CalendarViewType(); cldview.StartDate = dt.AddDays(-2); cldview.EndDate = dt.AddDays(2); findItemRequest.Item = cldview; findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
(3)定义服务器返回的数据属性
FindItemResponseType findItemResponse = _ExchangeBinding.FindItem(findItemRequest); ArrayOfResponseMessagesType responseMessages = findItemResponse.ResponseMessages; ResponseMessageType[] responseMessage = responseMessages.Items; foreach (ResponseMessageType rmt in responseMessage) { #region 结果 FindItemResponseMessageType msgType = (rmt as FindItemResponseMessageType); if (msgType.RootFolder == null) continue; FindItemParentType parentType = msgType.RootFolder; object obj = parentType.Item; if (obj is ArrayOfRealItemsType) { ArrayOfRealItemsType items = (obj as ArrayOfRealItemsType); if (items != null) { if (items.Items != null) { foreach (ItemType item in items.Items) { CalendarItemType calItem = item as CalendarItemType; DataRow row = table.NewRow(); row["roomid"] = 1; row["guid"] = guid; row["begintime"] = calItem.Start.AddHours(timezone); row["endtime"] = calItem.End.AddHours(timezone); row["location"] = calItem.Location; row["title"] = calItem.Subject; row["displayname"] = calItem.Organizer.Item.Name; if (row["endtime"].ToString() != "") { table.Rows.Add(row); } } } } } #endregion }