获取exchangeserve的calendar的item

在做项目中用到了这段代码。感觉不错。为了方便大家日后的查找,放在这里

http://weblogs.asp.net/psperanza/archive/2008/03.aspx

ContractedBlock.gif ExpandedBlockStart.gif Code
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;
  }

转载于:https://www.cnblogs.com/zsbfree/archive/2008/12/01/1345107.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值