博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Getting calendar items using Exchange Web Services
阅读量:4651 次
发布时间:2019-06-09

本文共 6363 字,大约阅读时间需要 21 分钟。

Getting calendar items using Exchange Web Services

I am no expert at exchange, let alone Exchange Web Services (EWS), but I recently had to use it to get at calendar information for a project. Let me tell you that the documentation for EWS sucks and the API is not very intuitive. That said, I was able to piece together what I needed. The code is not perfect and could definitely use a good once over by someone with more than 2 days experience.

The code will show how to get at calendar items, how to use a date range and how to get the body of the calendar item. I racked my brain for a while to get the body - you have to make a call to get the calendar items and then for each item you have to go and use the ID to get its body in another call. The idea is that the first call gets only the small parts of an item. If you want things like attachments then you make another call to get it. is a link to an article that gets into it.

The code is not perfect but I want to save someone the time that I spent. Please leave comments with suggestions and improvements.

UPDATE: I have attached some code. I don’t have access to an exchange server. It compiles but at least the classes are there for you.

private List<CalendarInfo> GetCalendarEvents()

{
List<CalendarInfo> calendarEvents = new List<CalendarInfo>();
ExchangeServiceBinding esb =
new ExchangeHelper().GetExchangeBinding("myUserName", "myPassword", "myDomain");
// Form the FindItem request.
FindItemType findItemRequest = new FindItemType();
CalendarViewType calendarView = new CalendarViewType();
calendarView.StartDate = DateTime.Now.AddDays(-1);
calendarView.EndDate = DateTime.Now.AddDays(1);
calendarView.MaxEntriesReturned = 100;
calendarView.MaxEntriesReturnedSpecified = true;
findItemRequest.Item = calendarView;
// Define which item properties are returned in the response.
ItemResponseShapeType itemProperties = new ItemResponseShapeType();
// Use the Default shape for the response.
//itemProperties.BaseShape = DefaultShapeNamesType.IdOnly;
itemProperties.BaseShape = DefaultShapeNamesType.AllProperties;
findItemRequest.ItemShape = itemProperties;
DistinguishedFolderIdType[] folderIDArray =
new DistinguishedFolderIdType[1];
folderIDArray[0] = new DistinguishedFolderIdType();
folderIDArray[0].Id = DistinguishedFolderIdNameType.calendar;
if (!string.IsNullOrEmpty(criteria.EmailAddress))
{
folderIDArray[0].Mailbox = new EmailAddressType();
folderIDArray[0].Mailbox.EmailAddress = "myEmail.com";
}
findItemRequest.ParentFolderIds = folderIDArray;
// Define the traversal type.
findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
try
{
// Send the FindItem request and get the response.
FindItemResponseType findItemResponse =
esb.FindItem(findItemRequest);
// Access the response message.
ArrayOfResponseMessagesType responseMessages =
findItemResponse.ResponseMessages;
ResponseMessageType[] rmta = responseMessages.Items;
int folderNumber = 0;
foreach (ResponseMessageType rmt in rmta)
{
// One FindItemResponseMessageType per folder searched.
FindItemResponseMessageType firmt =
rmt as FindItemResponseMessageType;
if (firmt.RootFolder == null)
continue ;
FindItemParentType fipt = firmt.RootFolder;
object obj = fipt.Item;
// FindItem contains an array of items.
if (obj is ArrayOfRealItemsType)
{
ArrayOfRealItemsType items =
(obj as ArrayOfRealItemsType);
if (items.Items == null)
{
folderNumber++;
}
else
{
foreach (ItemType it in items.Items)
{
if (it is CalendarItemType)
{
CalendarItemType cal = (CalendarItemType)it;
CalendarInfo ce = new CalendarInfo();
ce.Location = cal.Location;
ce.StartTime = cal.Start;
ce.EndTime = cal.End;
ce.Subject = cal.Subject;
ce.Body = GetMeetingBody(esb, cal);
calendarEvents.Add(ce);
}
}
folderNumber++;
}
}
}
}
catch (Exception e)
{
throw;
}
finally
{
}
return calendarEvents;
}
private string GetMeetingBody(ExchangeServiceBinding binding, CalendarItemType meeting)
{
string meetingBody = string.Empty;
CalendarItemType temp = null;
// Call GetItem on each ItemId to retrieve the
// item’s Body property and any AttachmentIds.
//
// Form the GetItem request.
GetItemType getItemRequest = new GetItemType();
getItemRequest.ItemShape = new ItemResponseShapeType();
// AllProperties on a GetItem request WILL return
// the message body.
getItemRequest.ItemShape.BaseShape =
DefaultShapeNamesType.AllProperties;
getItemRequest.ItemIds = new ItemIdType[1];
getItemRequest.ItemIds[0] = (BaseItemIdType)meeting.ItemId;
// Here is the call to exchange.
GetItemResponseType getItemResponse =
binding.GetItem(getItemRequest);
// We only passed in one ItemId to the GetItem
// request. Therefore, we can assume that
// we got at most one Item back.
ItemInfoResponseMessageType getItemResponseMessage =
getItemResponse.ResponseMessages.Items[0]
as ItemInfoResponseMessageType;
if (getItemResponseMessage != null)
{
if (getItemResponseMessage.ResponseClass ==
ResponseClassType.Success
&& getItemResponseMessage.Items.Items != null
&& getItemResponseMessage.Items.Items.Length > 0)
{
temp = (CalendarItemType)getItemResponseMessage.Items.Items[0];
if (temp.Body != null)
meetingBody = temp.Body.Value;
}
}
return meetingBody;
}

private ExchangeServiceBinding GetExchangeBinding(      string userName, string passwotrd, string domain)  {      ExchangeServiceBinding binding = new ExchangeServiceBinding();      ServicePointManager.ServerCertificateValidationCallback =              delegate(Object obj, X509Certificate certificate,               X509Chain chain, SslPolicyErrors errors)              {                  // Replace this line with code to validate server certificate.                  return true;              };      System.Net.WebProxy proxyObject =           new System.Net.WebProxy();      proxyObject.Credentials =           System.Net.CredentialCache.DefaultCredentials;      binding.Credentials =           new NetworkCredential(userName, password, domain);       string server = ConfigurationManager.AppSettings["ExchangeServer"] as string;      if (server == null || string.IsNullOrEmpty(server))          throw new ArgumentNullException("The Exchange server Url could not be found.");      binding.Url = server;      Console.WriteLine("***** " + server);      binding.Proxy = proxyObject;      return binding;  }
Attachment:
Posted: by | with
Filed under:

转载于:https://www.cnblogs.com/csts/archive/2012/04/26/2471025.html

你可能感兴趣的文章
华为上机试---购物单(算法:背包问题)
查看>>
PHP操作Mongodb API 及使用类 封装好的MongoDB操作类
查看>>
PHP实现经典算法
查看>>
NodeJS(四)Mac下如何安装package.json里面会产生依赖项
查看>>
MapReduce会自动忽略文件夹下的.开头的文件
查看>>
Android Learning:数据存储方案归纳与总结
查看>>
ACM题目————A simple problem
查看>>
Emmet的使用
查看>>
JAVA中Response的几种用法(设定时间调整到指定页面 ....... )
查看>>
java之sleep、wait、yield、join、notify乱解
查看>>
DEDECMS 关键字不能小于2个字节!
查看>>
Flutter学习笔记(10)--容器组件、图片组件
查看>>
gitlab 的使用策略和简单介绍
查看>>
Web.py Cookbook 简体中文版 - 保存上传的文件
查看>>
MongoDB学习笔记二—Shell操作
查看>>
Hibernate之二级缓存
查看>>
.NET Oracle连接方法
查看>>
浅谈数据库的完整性
查看>>
OSPF协议介绍及配置 (下)
查看>>
3. 从零开始学CSRF
查看>>