EWS.WebServices.Managed.Api

A short blog on the EWS Managed API and using the latest version with scripts leveraging Exchange Web Services (EWS), such as my Remove-DuplicateItems script. The installable EWS Managed API library was last updated in 2014 (version 2.2, reports as v15.0.913.22), and there have been few enhancements since then. These are included in the EWS.WebServices.Managed.Api package, which carries version 2.2.1.2.

Although this library was last updated in 2019, you might still need it to successfully run EWS scripts. The EWS.WebServices.Managed.Api package supports some Exchange Web Services calls which are not supported in the 2.2 version, and may lead to error messages like Exception calling “FindFolders” with “2” argument(s) or other messages related to the (number of) arguments. This may be an indication a particular call was used to one of the EWS functions, but which is not supported by the installed EWS Managed API library. In those cases, installing this updated library might help.

The library is published on NUGet as a package. To install the package, we first need to register NuGet as a Package Source:

Register-PackageSource -provider NuGet -name nugetRepository -location https://www.nuget.org/api/v2

Next, install the package from the newly defined NuGet source:

Install-Package Exchange.WebServices.Managed.Api

The package installs by default under C:\Program Files\PackageManagement\NuGet\Packages. I have updated my scripts to include this location when searching for the required Microsoft.Exchange.WebServices.dll as well. Alternatively, you can copy this DLL from the ..\Exchange.WebServices.Managed.Api.<Version>\lib\net35 folder to the location where the script resides. When running my EWS-based scripts in Verbose mode, it will report as version 2.2.1.0.

Hopefully this blog will potentially save you some time troubleshooting, and myself answering some support messages. Enjoy!

This entry was posted in Exchange Server and tagged , by Michel de Rooij. Bookmark the permalink.
Unknown's avatar

About Michel de Rooij

Michel de Rooij, with over 25 years of mixed consulting and automation experience with Exchange and related technologies, is a consultant for Rapid Circle. He assists organizations in their journey to and using Microsoft 365, primarily focusing on Exchange and associated technologies and automating processes using PowerShell or Graph. Michel's authorship of several Exchange books and role in the Office 365 for IT Pros author team are a testament to his knowledge. Besides writing for Practical365.com, he maintains a blog on eightwone.com with supporting scripts on GitHub. Michel has been a Microsoft MVP since 2013.

32 thoughts on “EWS.WebServices.Managed.Api

  1. Michel
    working with Remove-Duplicates update and got a few errors.
    When I updated to EWS2.2.1.0 , i get errors with SearchFilter+IsEqualTo
    New-Object : Cannot find an overload for “IsEqualTo” and the argument count: “2”.
    If i revert to EWS15, SearchFilter works as expected.
    Any thoughts ?

    Like

    • When running in Verbose mode, does it say “VERBOSE: Module Microsoft.Exchange.WebServices v2.2.1.0 loaded” or did it find and load a module somewhere else (mentioned in line before, starting with “Loading module from path”)? The search order is:
      1) Current folder where script runs from
      2) Location where package is installed
      Note that once loaded, the script doesn’t unload the module. Hence you may need to remove (unload) it, or close the PS session and start a new one.

      Like

      • correct. the error appears only with 2.2.1 , which i used in an attempt to overcome an issue with properties not supplorted by EXS2013, but do appear in the current nuget package.

        [[VERBOSE: Module Microsoft.Exchange.WebServices v2.2.1.0 already loaded
        VERBOSE: Module Microsoft.Identity.Client v4.31.0.0 already loaded
        VERBOSE: Using credentials filemail
        VERBOSE: Cleanup Mode: Folder]]

        I have rewitten your Get_Folders logic with the result below
        Function Get-SubFolder {
        param (
        $ParentFolder,
        $sFolderName
        )
        $FolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView( 1)
        #$FolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Shallow
        #$FolderView.PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet(
        # [Microsoft.Exchange.WebServices.Data.BasePropertySet]::IdOnly,
        # [Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName)
        $SearchFilter = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName, $sFolderName)
        $FolderResults = myEWSFind-Folders $EwsService $ParentFolder.Id $SearchFilter $FolderView

        —————–
        error: Cannot find an overload for “IsEqualTo” and the argument count: “2”.
        At line:1 char:1
        + ./Remove-DuplicateItems203rg1.ps1 -Identity FileMail -PublicFolders …
        + ~~~~~~~~~~

        if i use the PropertySet it errors with.
        New-Object : Cannot find an overload for “PropertySet” and the argument count: “3”.
        At C:\Users\gray\Dropbox\devel\Exchange\Remove-DuplicateItems203rg2.ps1:1110 char:30
        + … ropertySet= New-Object Microsoft.Exchange.WebServices.Data.PropertySe …
        + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

        i have reverted script in use back to EWS15.0 for now.
        thanks

        Like

          • your update may have fixed something. only errors now relate to the FindItems
            perhaps Hashtags and Mentions are FirstClassProperties ?
            PS C:\Users\mmmm\Dropbox\devel\Exchange> .\Remove-DuplicateItems205rg1.ps1 -Identity myname -PublicFolders -PFStart “_Projects\2021” -Credentials $Credentials -Impersonation -Report -Force -Verbose
            VERBOSE: Module Microsoft.Exchange.WebServices v2.2.1.2 already loaded
            VERBOSE: Module Microsoft.Identity.Client v4.31.0.0 already loaded
            VERBOSE: Using credentials MyName
            VERBOSE: Cleanup Mode: Folder
            Processing Public folders [_Projects\2021] as MyName@MyOrg.com.au (MyName)
            VERBOSE: Using MyName@MyOrg.com.au for impersonation
            VERBOSE: Looking up EWS URL using Autodiscover for MyName@MyOrg.com.au
            VERBOSE: Using EWS endpoint https://mail.MyOrg.com.au/EWS/Exchange.asmx
            VERBOSE: Constructing folder matching rules
            VERBOSE: Processing public folders
            VERBOSE: …finding _Projects\2021
            VERBOSE: …found root IPM_SUBTREE
            Finding subfolder..IPM_SUBTREE _Projects
            VERBOSE: Folderview created :100
            VERBOSE: fnd-IPM_SUBTREE\_Projects
            Finding subfolder.._Projects 2021
            VERBOSE: Folderview created :100
            VERBOSE: fnd-_Projects\2006
            VERBOSE: fnd-_Projects\2008
            VERBOSE: fnd-_Projects\2010
            VERBOSE: fnd-_Projects\2011
            VERBOSE: fnd-_Projects\2012
            VERBOSE: fnd-_Projects\2013
            VERBOSE: fnd-_Projects\2014
            VERBOSE: fnd-_Projects\2015
            VERBOSE: fnd-_Projects\2016
            VERBOSE: fnd-_Projects\2017
            VERBOSE: fnd-_Projects\2018
            VERBOSE: fnd-_Projects\2019
            VERBOSE: fnd-_Projects\2020
            VERBOSE: fnd-_Projects\2021
            VERBOSE: Rootfolders _Projects\2021 2021
            VERBOSE: Collecting folders to process..
            VERBOSE: Adding folder \21001 (priority 0)
            VERBOSE: Adding folder \21001\21001_Contacts (priority 0)

            Processing folder \21114\CO
            WARNING: Error performing operation FindItems without Search options in CO. Error Exception calling “FindItems” with “1” argument(s): “The property Hashtags is valid only for Exchange Exchang
            e2015 or later versions.”
            VERBOSE: Cleaning unique list (finished folder)

            Processing folder \21124\CO
            WARNING: Error performing operation FindItems without Search options in CO. Error Exception calling “FindItems” with “1” argument(s): “The property Mentions is valid only for Exchange Exchang
            e2015 or later versions.”
            ——————————
            Function myEWSFind-ItemsNoSearch {
            param(
            [Microsoft.Exchange.WebServices.Data.Folder]$Folder,
            [Microsoft.Exchange.WebServices.Data.ItemView]$ItemView
            )
            $OpSuccess= $false
            $CritErr= $false
            Do {
            Try {
            $res= $Folder.FindItems( $ItemView)
            $OpSuccess= $true
            }
            catch [Microsoft.Exchange.WebServices.Data.ServerBusyException] {
            $OpSuccess= $false
            Write-Warning ‘EWS operation failed, server busy – will retry later’
            }
            catch {
            $OpSuccess= $false
            $critErr= $true
            Write-Warning (‘Error performing operation FindItems without Search options in {0}. Error {1}’ -f $Folder.DisplayName, $Error[0])
            }

            Like

          • Hi Rob,

            1) My public release doesn’t yet include support for PublicFolders; don’t know where you got this copy from (or added it yourself).
            2) As mentioned in the error message, your version of the script seems to access a property “Mentions”, which isn’t available. Without explicitly retrieving it, I can only find some reports of FirstClassProperties trying to retrieve these attributes for some odd reason.

            Like

  2. Michel.. I did some further testing as below using different versions of EWS.dll; I compliled 2.2.1.2 from the git source
    the propertyset appears to have an issue with EWS2.2.1 but not with 2.2.0 aka 15.0.0.0913 which works as expected. (as you noted i had to reload the shell each time to use different version)
    I may try tests using c# later.
    >>>>>—extest1.ps1———
    #$EWSDLL = “C:\users\gray\dropbox\devel\Exchange\Microsoft.Exchange.WebServices15-0913.dll”
    #$EWSDLL = “C:\users\gray\dropbox\devel\Exchange\Microsoft.Exchange.WebServices2212.dll”
    $EWSDLL = “C:\users\gray\dropbox\devel\Exchange\Microsoft.Exchange.WebServices2210.dll”
    import-module $EWSDLL

    $DLLObj = Get-ChildItem -Path “$EWSDLL” -ErrorAction SilentlyContinue
    If ( $DLLObj) {
    Write-Host (‘Loaded EWS Managed API v{0}’ -f $DLLObj.VersionInfo.FileVersion)
    }

    #test1
    Write-Host (‘create folderview’)
    $FolderView= New-Object Microsoft.Exchange.WebServices.Data.FolderView( 100)
    $FolderView.Traversal= [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Shallow
    $folderview

    Write-Host (‘Folderview created :{0} >{1}>>>>——-output from test ————————

    [PS] C:\Users\gray\Dropbox\devel\Exchange>./exstest1.ps1
    Loaded EWS Managed API v15.00.0913.015
    create folderview

    Traversal : Shallow
    PageSize : 100
    OffsetBasePoint : Beginning
    Offset : 0
    PropertySet :

    Folderview created :100 >0
    ——————————–
    [PS] C:\WINDOWS\system32>cd C:\Users\gray\Dropbox\devel\Exchange
    [PS] C:\Users\gray\Dropbox\devel\Exchange>./exstest1.ps1
    Loaded EWS Managed API v2.2.1.2
    create folderview

    Traversal : Shallow
    PageSize : 100
    OffsetBasePoint : Beginning
    Offset : 0
    PropertySet :

    Folderview created :100 >0
    ====================================================================
    [PS] C:\Users\gray\Dropbox\devel\Exchange>./exstest1.ps1
    Loaded EWS Managed API v2.2.1.0
    create folderview

    Traversal : Shallow
    PageSize : 100
    OffsetBasePoint : Beginning
    Offset : 0
    PropertySet :

    Folderview created :100 >0<
    Propset0 0
    New-Object : Cannot find an overload for "PropertySet" and the argument count: "3".
    At C:\Users\gray\Dropbox\devel\Exchange\exstest1.ps1:25 char:9
    + $pset = New-Object Microsoft.Exchange.WebServices.Data.PropertySet(
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

    Propset1 0
    Folderview 2 :100 0
    New-Object : Cannot find an overload for "PropertySet" and the argument count: "3".
    At C:\Users\gray\Dropbox\devel\Exchange\exstest1.ps1:35 char:26
    + … ropertySet= New-Object Microsoft.Exchange.WebServices.Data.PropertySe …

    Like

    • sorry. output from first test didnt get inserted properly.
      Loaded EWS Managed API v15.00.0913.015
      create folderview

      Traversal : Shallow
      PageSize : 100
      OffsetBasePoint : Beginning
      Offset : 0
      PropertySet :

      Folderview created :100 >0

      Like

  3. EWS2.2.1.2 works as expected from a c# desktop app.
    pset = new PropertySet(BasePropertySet.IdOnly, FolderSchema.DisplayName,FolderSchema.FolderClass);
    fv.PropertySet = pset;
    Debug.Print(“Folderview reformed :{0} >{1}<", fv.PageSize, fv.PropertySet.Count);

    Like

  4. Any suggestions on this error:

    WARNING: Cannot bind to MsgFolderRoot: Exception calling “Bind” with “2” argument(s): “The specified object was not found in the store., The process failed to get the correct properties.”

    Like

  5. for now, replacing FirstClassProperties with Base + needed properties removes the errors

    $ItemView.PropertySet= New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::IdOnly)
    $ItemView.PropertySet.Add( [Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived)
    $ItemView.PropertySet.Add( [Microsoft.Exchange.WebServices.Data.ItemSchema]::Subject)
    $ItemView.PropertySet.Add( [Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::InternetMessageId)
    $ItemView.PropertySet.Add( [Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeSent)
    $ItemView.PropertySet.Add( [Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::Sender)
    $ItemView.PropertySet.Add( [Microsoft.Exchange.WebServices.Data.ItemSchema]::Size)

    $ItemView.PropertySet.Add( [Microsoft.Exchange.WebServices.Data.ContactSchema]::FileAs)
    $ItemView.PropertySet.Add( [Microsoft.Exchange.WebServices.Data.ContactSchema]::GivenName)
    $ItemView.PropertySet.Add( [Microsoft.Exchange.WebServices.Data.ContactSchema]::Surname)
    $ItemView.PropertySet.Add( [Microsoft.Exchange.WebServices.Data.ContactSchema]::CompanyName)

    $ItemView.PropertySet.Add( [Microsoft.Exchange.WebServices.Data.ContactSchema]::BusinessPhone)
    $ItemView.PropertySet.Add( [Microsoft.Exchange.WebServices.Data.ContactSchema]::HomePhone)
    $ItemView.PropertySet.Add( [Microsoft.Exchange.WebServices.Data.ContactSchema]::MobilePhone)

    Like

  6. Hello,

    when I run the script, I get this error message:

    WARNUNG: Cannot bind to MsgFolderRoot: Ausnahme beim Aufrufen von “Bind” mit 2 Argument(en): “The request failed. Die zugrunde liegende Verbindung wurde
    geschlossen: Für den geschützten SSL/TLS-Kanal konnte keine Vertrauensstellung hergestellt werden..”
    WARNUNG: Cannot bind to ArchiveMsgFolderRoot: Ausnahme beim Aufrufen von “Bind” mit 2 Argument(en): “The request failed. Die zugrunde liegende
    Verbindung wurde geschlossen: Für den geschützten SSL/TLS-Kanal konnte keine Vertrauensstellung hergestellt werden..”

    It is a mixure of german and english, but I hope someone can help.

    Thanks
    Michi

    Like

    • Hi,

      I could solve this and another issue by executing this commands before running the script:

      $AllProtocols = [System.Net.SecurityProtocolType]’Tls11,Tls12′
      [System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
      [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

      But our Exchange is in German. So I get now this error:

      C:\scripts\Remove-DuplicateItems.ps1 : Cannot access mailbox information store for user@mailbox.com: Ausnahme beim Festlegen von
      “PropertySet”: “Der Wert “Microsoft.Exchange.WebServices.Data.PropertySet” vom Typ “Microsoft.Exchange.WebServices.Data.PropertySet” kann nicht in den
      Typ “Microsoft.Exchange.WebServices.Data.PropertySet” konvertiert werden.”
      In Zeile:1 Zeichen:1
      + .\Remove-DuplicateItems.ps1 -Identity adimter -Type Calendar -DeleteM …
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
      + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Remove-DuplicateItems.ps1

      Will this run on a german Exchange?

      If not, which parameters should I update?

      Thanks
      Michi

      Like

      • Hi Michi,
        Is this against a mailbox in Exchange on-prem or Exchange Online? If on-prem, what version?
        Also, what version of the EWS and Identity libraries have you installed (run the script with -Verbose to see it mention the version numbers at the top).

        Like

  7. I am getting an error when trying literally any variation of commands with this script. The output it always the following:
    VERBOSE: Module Microsoft.Exchange.WebServices v2.2.1.0 already loaded
    VERBOSE: Loading module C:\Program Files\PackageManagement\NuGet\Packages\Microsoft.Identity.Client.4.45.0\lib\monoandroid10.0\Microsoft.Identity.Client.dll
    VERBOSE: Loading module from path ‘C:\Program Files\PackageManagement\NuGet\Packages\Microsoft.Identity.Client.4.45.0\lib\monoandroid10.0\Microsoft.Identity.Client.dll’.
    Write-Error: C:\Scripts\Remove-DuplicateItems.ps1:1481
    Line |
    1481 | Import-ModuleDLL -Name ‘Microsoft.Identity.Client’ -FileName ‘Mic …
    | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    | Problem loading module Microsoft.Identity.Client: Assembly with same name is already loaded

    Like

    • Hi,
      the topic is not imporant anymore.
      I exported the data to PST, cleared the mailbox and imported again via Exchange Powershell by scipping duplicate items.

      cu
      Michi

      Like

    • Why is it trying to load an Android module? Perhaps the module location order is playing you part; which module was/is already loaded (Get-Module Microsoft.Identity.Client -ListAvailable | fl )

      Like

      • I honestly have no idea why it is trying to load an Android Module. All I have done is follow instructions as closely as possible; Perhaps something from one of the other projects I have done using Android Studio is bleeding over. I am going to try on a different PC

        Like

  8. Hello, I’m getting this error. Any thoughts or ideas to fix?

    Import-ModuleDLL : Problem loading module Microsoft.Exchange.WebServices: Could not load file or assembly ‘file:///C:sourceExchange Remove
    Duplicates PS ScriptMicrosoft.Exchange.WebServices.dll’ or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
    At C:sourceExchange Remove Duplicates PS ScriptRemove-DuplicateItems.ps1:1559 char:5

    • Import-ModuleDLL -Name ‘Microsoft.Exchange.WebServices’ -FileName …
    • ~~~~~~~~~~~~~

      • CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
      • FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Import-ModuleDLL

    Like

  9. thank you. I happened to drop the script into another directory for an application that also uses the dll’s. I know this is not how it’s meant to run, but it appears to be running. However, I get these messages as warnings. Is this something I can ignore? 

    WARNING: Cannot bind to MsgFolderRoot: Cannot find an overload for “Bind” and the argument count: “2”.WARNING: Cannot bind to ArchiveMsgFolderRoot: Cannot find an overload for “Bind” and the argument count: “2”.

    Like

  10. Hello. When running a script after “VERBOSE: Will use provided secret to autenticate” I get an error:
    The following exception occurred while retrieving member “WithClientSecret”: “Could not load file or assembly ‘Microsoft.IdentityModel.Abstractions, Version=6.35.0.0, Culture=neutral, PublicKeyToken=31bf3
    856ad33ffs4′ or one of its dependencies. The system cannot find the file specified.”
    At C:UnarchiveInvoke-Unarchive.ps1:857 char:13

    • $App= [Microsoft.Identity.Client.ConfidentialClientApplic …
    • ~~~~~~~~~~~~~

      • CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException
      • FullyQualifiedErrorId : CatchFromBaseGetMember

    C:UnarchiveInvoke-Unarchive.ps1 : Problem acquiring token: You cannot call a method on a null-valued expression.At C:UnarchiveUntitled1.ps1:5 char:1

    FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-Unarchive.ps1

    .Invoke-Unarchive.ps1 -Identity username@domain.com -Server outlook.o …

    ~~~~~~~~~~~~~~~~~

    CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException

    Any idea where’s the problem? Also what permissions should app registration have in Azure? Thanks!

    Like

  11. Thanks for the help. I’m trying to run the script on a Windows 2019 server with EX 2019 Tools installed to remove duplicate items from our Exchange Online environment.

    The command ‘Remove-DuplicateItems-master> .Remove-DuplicateItems.ps1 -identity xxx@xx.com -Type All -DeleteMode MoveToDeletedItems -Mode Full -Impersonation -UseDefaultCredentials:$false’ is getting the error.

    “Import-ModuleDLL : Problem loading module Microsoft.Exchange.WebServices: Could not load file or assembly ‘file:///C:U
    sersuadminDownloadsRemove-DuplicateItems-masterRemove-DuplicateItems-masterMicrosoft.Exchange.WebServices.dll’ or
    one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
    At C:UsersuadminDownloadsRemove-DuplicateItems-masterRemove-DuplicateItems-masterRemove-DuplicateItems.ps1:1559
    char:5

    • Import-ModuleDLL -Name ‘Microsoft.Exchange.WebServices’ -FileName …
    • ~~~~~~~~~~~~~

      • CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
      • FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Import-ModuleDLL”

    The registration and install command seem to have succeeded for Microsoft.Exchange.WebServices.dll and Microsoft.Exchange.WebServices.xml as the DLL and XML and other files are present in the script directory. However the import with-in the script execution is failing. I am running PS as an administrator.

    Does the script need to run on with Windows and Exchange 2016? Or what might resolve the issue on Windows and EX 2019?

    Thanks very much for your assistance.

    Like

    • Loading external DLLs is sometimes restricted and you need to use an elevated PSH session.
      Also, EXO required OAuth, so you need to register an app and use its AppId together with TenantId and certificiate or secret to authenticate.

      Like

Leave a reply to Michi Cancel reply