다음을 통해 공유


How to do FindItem and GetItem Operations of Exchange Web Services using VB.net

It does seem to be a bit of a dearth of VB.net samples for Exchange Web Service. So here is a sample Vb.net code which demonstrates:

  • FindItem(How to perform FindItem Operation to list all items from the inbox folder of a use mailbox)
  • GetItem (How to perform GetItem Operation to get an item with all properties)

NOTE: Following programming examples is for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This sample code assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. This sample code is provided for the purpose of illustration only and is not intended to be used in a production environment. 

 'List all the items from the inbox folder
  Private Function ListFolderItems(ByRef esb As ExchangeServiceBinding) As List(Of MessageType)
         Dim Messages As New List(Of MessageType)
         Messages = Nothing
  
         ' Form the FindItem request
         Dim findRequest As FindItemType = New FindItemType()
         findRequest.Traversal = ItemQueryTraversalType.Shallow
  
         ' Define which item properties are returned in the response
         Dim itemProperties As ItemResponseShapeType = New ItemResponseShapeType()
         itemProperties.BaseShape = DefaultShapeNamesType.AllProperties
  
         ' Add properties shape to request
         findRequest.ItemShape = itemProperties
  
         ' Identify which folders to search to find items
         Dim folderIDArray() As DistinguishedFolderIdType = New DistinguishedFolderIdType(1) {}
         folderIDArray(0) = New DistinguishedFolderIdType()
         folderIDArray(0).Id = DistinguishedFolderIdNameType.inbox
  
         ' Add folders to request
         findRequest.ParentFolderIds = folderIDArray
  
  
         'Send the listing (find) request and get the response
         Dim findResp As New FindItemResponseType
         findResp = esb.FindItem(findRequest)
  
         ' Get the response messages
         Dim responseMessage As ResponseMessageType
         For Each responseMessage In findResp.ResponseMessages.Items
             ' Cast to the correct response message type
             Dim findItemResponseMessage As FindItemResponseMessageType = responseMessage
             If findItemResponseMessage.ResponseClass = ResponseClassType.Success Then
  
                 ' Get the actual payload of items
                 Dim realItems As ArrayOfRealItemsType = findItemResponseMessage.RootFolder.Item
                 If Not realItems.Items Is Nothing Then
                     ' Initialize list of messages and fill
                     Messages = New List(Of MessageType)(realItems.Items.Length)
                     Dim message As MessageType = Nothing
                     Dim item As ItemType = Nothing
                     For Each item In realItems.Items
                         If TypeOf item Is MessageType Then
                             message = item
                             Messages.Add(message)
                         End If
                     Next
                 End If
             End If
         Next
         Return Messages
     End Function
  
 'To get a specific item based on ItemId
 Public Shared Function GetItem(ByVal serviceBinding As ExchangeServiceBinding, ByVal itemID As String) As ItemType
         ' Form the GetItem request
         Dim getRequest As GetItemType = New GetItemType()
  
         ' Define which item properties are returned in the response
         Dim itemProperties As ItemResponseShapeType = New ItemResponseShapeType()
         itemProperties.BaseShape = DefaultShapeNamesType.AllProperties
  
         ' Add properties shape to request
         getRequest.ItemShape = itemProperties
  
         ' Set the itemID of the desired item to retrieve
         Dim id As ItemIdType = New ItemIdType()
         id.Id = itemID
  
         getRequest.ItemIds = New ItemIdType() {id}
         ' Send the listing (find) request and get the response
         Dim getResp As New GetItemResponseType
         getResp = serviceBinding.GetItem(getRequest)
  
         ' Get the response message
         If getResp.ResponseMessages.Items(0).ResponseClass = ResponseClassType.Success Then
             Dim iirmt As ItemInfoResponseMessageType = getResp.ResponseMessages.Items(0)
             If iirmt.Items.Items.Length > 0 Then
                 Return iirmt.Items.Items(0)
             Else
                 Return Nothing
             End If
         Else
             Return Nothing
         End If
     End Function
  
  

Here are the few link related to EWS for basis understanding :

Please share you comments or questions with me related to subjects on my blog. I would like to explore the messaging world further with you...

Comments

  • Anonymous
    February 06, 2009
    PingBack from http://www.clickandsolve.com/?p=4396
  • Anonymous
    April 28, 2009
    In continuations of my previous post How to do FindItem and GetItem Operations of Exchange Web Services