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!

17 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

  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

Leave a Reply to Dean Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.