How to do FindFolder and MoveItem Operations of Exchange Web Services using

In continuations of my previous post How to do FindItem and GetItem Operations of Exchange Web Services using ; Here is the Exchange Web Services sample in to perform following task:

  • FindFolder(How to perform FindFolder Operation to get FolderId of specific folder for the root of the mailbox)
  • MoveItem (How to perform MoveItem Operation to move an item to the another folder within the mailbox)

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. 

Private Sub cmdMove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMove.Click
            If lstMails.SelectedItems.Count = 1 Then
                Dim itemID As New ItemIdType
                itemID.Id = lstMails.SelectedItems(0).SubItems(3).Text
                'Find the folderId to which we want to move item
                Dim parentFolder As New DistinguishedFolderIdType
                parentFolder.Id = DistinguishedFolderIdNameType.root
                Dim tfTargetFolder As New FolderIdType
                'Change the name of the folder we are searching for FolderID
                tfTargetFolder = FindFolder(ServiceBinding, parentFolder, "Test")
                Dim newItemID As New ItemIdType
                'Move am Item based on ItemId and FolderID
                newItemID = MoveItemtoTest(ServiceBinding, itemID.Id, tfTargetFolder)
                Dim strId As String
                strId = "New ItemID " + newItemID.Id.ToString
                MessageBox.Show(strId, "Item Moved", MessageBoxButtons.OK, MessageBoxIcon.Information)
                MessageBox.Show("Please select a item in list to move", "Item to Move", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
        End Try
    End Sub
Public Shared Function FindFolder(ByVal serviceBinding As ExchangeServiceBinding, ByVal fiFolderID As DistinguishedFolderIdType, ByVal fnFldName As String) As FolderIdType
        Dim rvFolderID As New FolderIdType
        ' Create the request and specify the travesal type
        Dim findFolderRequest As FindFolderType = New FindFolderType()
        findFolderRequest.Traversal = FolderQueryTraversalType.Deep
        ' Define the properties returned in the response
        Dim responseShape As FolderResponseShapeType = New FolderResponseShapeType()
        responseShape.BaseShape = DefaultShapeNamesType.Default
        findFolderRequest.FolderShape = responseShape
        ' Identify which folders to search
        Dim folderIDArray() As DistinguishedFolderIdType = New DistinguishedFolderIdType(1) {}
        folderIDArray(0) = New DistinguishedFolderIdType()
        folderIDArray(0).Id = fiFolderID.Id
        'Add Restriction for DisplayName
        Dim ffRestriction As New RestrictionType
        Dim ieToType As New IsEqualToType
        Dim diDisplayName As New PathToUnindexedFieldType
        diDisplayName.FieldURI = UnindexedFieldURIType.folderDisplayName
        Dim ciConstantType As New FieldURIOrConstantType
        Dim cvConstantValueType As New ConstantValueType
        cvConstantValueType.Value = fnFldName
        ciConstantType.Item = cvConstantValueType
        ieToType.Item = diDisplayName
        ieToType.FieldURIOrConstant = ciConstantType
        ffRestriction.Item = ieToType
        findFolderRequest.Restriction = ffRestriction
        ' Add the folders to search to the request
        findFolderRequest.ParentFolderIds = folderIDArray
            ' Send the request and get the response
            Dim findFolderResponse As FindFolderResponseType = serviceBinding.FindFolder(findFolderRequest)
            ' Get the response messages
            If findFolderResponse.ResponseMessages.Items(0).ResponseClass = ResponseClassType.Error Then
                MessageBox.Show("Error Occured")
                Return Nothing
                Dim rmta() As ResponseMessageType = findFolderResponse.ResponseMessages.Items
                Dim rmt As ResponseMessageType
                For Each rmt In rmta
                    ' Cast to the correct response message type
                    Dim ffResponse As FindFolderResponseMessageType = rmt
                    Dim fFoundFolder As FolderType
                    For Each fFoundFolder In ffResponse.RootFolder.Folders
                        rvFolderID = fFoundFolder.FolderId
                'Return the FolderID of the last folder found
                Return (rvFolderID)
            End If
        Catch e As Exception
            Return Nothing
        End Try
    End Function
Public Shared Function MoveItemtoTest(ByVal serviceBinding As ExchangeServiceBinding, ByVal itemID As String, ByVal trgfldID As FolderIdType) As ItemIdType
        'Setup FolderId and ItemId to be passed to MoveItem
        Dim tfTargetFolder As TargetFolderIdType = New TargetFolderIdType()
        tfTargetFolder.Item = trgfldID
        Dim iiItemId As ItemIdType = New ItemIdType()
        iiItemId.Id = itemID
        'Create request to move Item and specify properties
        Dim miMoveItemRequest As MoveItemType = New MoveItemType()
        miMoveItemRequest.ItemIds = New ItemIdType(1) {}
        miMoveItemRequest.ItemIds(0) = iiItemId
        miMoveItemRequest.ToFolderId = tfTargetFolder
        Dim nID As New ItemIdType
            ' Send the request and get the response
            Dim miResponse As MoveItemResponseType = serviceBinding.MoveItem(miMoveItemRequest)
            If miResponse.ResponseMessages.Items(0).ResponseClass = ResponseClassType.Error Then
                MessageBox.Show("Error Occured")
                Return Nothing
                Dim iirmt As ItemInfoResponseMessageType = miResponse.ResponseMessages.Items(0)
                If iirmt.Items.Items.Length > 0 Then
                    'Get updated ItemId from the Response Message
                    nID = iirmt.Items.Items(0).ItemId
                    MessageBox.Show("Item Moved")
                    'Return updated ItemID
                    Return nID
                    Return Nothing
                End If
            End If
        Catch e As Exception
            Return Nothing
        End Try
    End Function

We can refer to the following articles related to the EWS:

While we can workout more with Exchange Web Service auto generated proxies but don’t forget to have look at Microsoft Exchange Web Services (EWS) Managed API 1.0 to do stuff with less sweat. 🙂

Skip to main content