Question

I am converting some code that was written by a former employee using the Exchange Web Services API from Exchange 2007. When it was written, we had an on-premise Exchange 2007 Server. Now we have moved our email to Exchange Online and I want to use the Exchange Web Services for Exchange 2013 (API 2.1).

The old code used the ExchangeWebServices namespace and I want to use the Microsoft.Exchange.Webservices namespace in the new v2.1 API

None of the references from the old ExchangeWebServices namespace are defined in the new Microsoft.Exchange.Webservices so I need to refactor the code to match equivalent items in the new API.

One of the things I need to do is create a list of all of the Message IDs of the items in the Inbox for a specific user account. In the old code, this was used;

Private ExchangeBinding As New ExchangeServiceBinding
Public Sub New(ByVal UserName As String, ByVal Password As String, ByVal Domain As String, ByVal URL As String)
    ExchangeBinding.Credentials = New NetworkCredential(UserName, Password, Domain)
    ExchangeBinding.Url = URL
End Sub

Public Function GetInboxMessageIDs() As ArrayOfRealItemsType

    Dim returnInboxMessageIds As ArrayOfRealItemsType = Nothing
    Dim errMsg As String = String.Empty

    'Create the request and specify the travesal type.
    Dim FindItemRequest As FindItemType
    FindItemRequest = New FindItemType
    FindItemRequest.Traversal = ItemQueryTraversalType.Shallow

    'Define which item properties are returned in the response.
    Dim ItemProperties As ItemResponseShapeType
    ItemProperties = New ItemResponseShapeType
    ItemProperties.BaseShape = DefaultShapeNamesType.IdOnly

    'Add properties shape to the request.
    FindItemRequest.ItemShape = ItemProperties

    'Identify which folders to search to find items.
    Dim FolderIDArray(0) As DistinguishedFolderIdType
    FolderIDArray(0) = New DistinguishedFolderIdType
    FolderIDArray(0).Id = DistinguishedFolderIdNameType.inbox

    'Add folders to the request.
    FindItemRequest.ParentFolderIds = FolderIDArray

    Try
        'Send the request and get the response.
        Dim FindItemResponse As FindItemResponseType
        FindItemResponse = ExchangeBinding.FindItem(FindItemRequest)

        'Get the response messages.
        Dim ResponseMessage As ResponseMessageType()
        ResponseMessage = FindItemResponse.ResponseMessages.Items

        Dim FindItemResponseMessage As FindItemResponseMessageType

        If ResponseMessage(0).ResponseClass = ResponseClassType.Success Then

            FindItemResponseMessage = ResponseMessage(0)
            returnInboxMessageIds = FindItemResponseMessage.RootFolder.Item

        Else

            '' Server error
            Dim responseClassStr As String = [Enum].GetName(GetType(ExchangeWebServices.ResponseClassType), ResponseMessage(0).ResponseClass).ToString
            Dim responseCodeStr As String = [Enum].GetName(GetType(ExchangeWebServices.ResponseCodeType), ResponseMessage(0).ResponseCode).ToString
            Dim messageTextStr As String = ResponseMessage(0).MessageText.ToString
            Dim thisErrMsg As String = String.Format("ExchangeWebServices Inbox Error: {0}, {1}, {2}", responseClassStr, responseCodeStr, messageTextStr)
            errMsg = If(errMsg.Equals(String.Empty), String.Empty, errMsg & "; ") & thisErrMsg

        End If

    Catch ex As Exception
        errMsg = If(errMsg.Equals(String.Empty), String.Empty, errMsg & "; ") & ex.Message
    End Try

    If Not errMsg.Equals(String.Empty) Then
        returnInboxMessageIds = Nothing
        Throw New System.Exception(errMsg)
    End If

    Return returnInboxMessageIds

End Function

The items in the old API namespace that are not referenced in the new API namespace are;

-ExchangeServiceBinding
-ArrayOfRealItemsType
-FindItemType
-ItemQueryTraversalType
-ItemResponseShapeType
-DefaultShapeNamesType
-DistinguishedFolderIdType
-DistinguishedFolderIdNameType
-FindItemResponseType
-ResponseMessageType()
-FindItemResponseMessageType
-ResponseClassType

Does anyone have any recommendations for achieving this result using the new API code?

Was it helpful?

Solution

The API that was used in your earlier application was based off the generated proxy from EWS. The new API you are referring to is the EWS Managed API which is a wrapper around EWS and is the recommended way to use EWS.

To answer your first question about getting the message id for each message in the Inbox, here is a link to a 'How to' topic that should help you get started. It talks about getting items in batches because if you are like me then there are a lot of messages in the Inbox at any given time. You can set the number of items to return, but the example shows doing only five at a time. And I know it's in C#, but it should easily convert to VB.

Example: Perform a paged search by using the EWS Managed API

Regarding items in the namespace that are missing. They aren't actually missing, they have been renamed during the development of the managed API. Here is the list (with links) to the new topics that match what you were using in the older code:

There are a lot of 'How to' topics in the Exchange Online and Exchange 2013 development documentation in the Develop web service clients for Exchange node. These focus on EWS Managed API and also give examples of EWS with SOAP.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top