设为首页 收藏本站
查看: 2278|回复: 0

[经验分享] Getting calendar items using Exchange Web Services

[复制链接]

尚未签到

发表于 2015-9-11 07:44:50 | 显示全部楼层 |阅读模式
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. Here 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: Test.ExchangeServices.zip
Posted:Mar 18 2008, 06:59 PM by Paul Speranza | with 71 comment(s)

Filed under: .Net C# Exchange Calendar items

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-112074-1-1.html 上篇帖子: EWS编程问题二:如何利用EWS读取Exchange里面相应用户的未读邮件数量(解决) 下篇帖子: Exchange 常见问题之二
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表