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!

7 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

  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

Leave a 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.