OfficeDev / ews-managed-api

Other
583 stars 317 forks source link

EWS RecipientCache and Suggested Contacts fail itemShape is nul #288

Closed jrouzies-fr closed 1 year ago

jrouzies-fr commented 1 year ago

Hello,

I am trying to use EWS to clear some user contact cache he has that doesn't want to go away even with the clear on Outlook.

$FolderId= New-Object Microsoft.Exchange.WebServices.Data.FolderId( [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::RecipientCache)  
$RecipientCacheFolder= [Microsoft.Exchange.WebServices.Data.Folder]::Bind( $EwsService, $FolderId)

$ItemView= New-Object Microsoft.Exchange.WebServices.Data.FolderView( 1000)
$ItemView.PropertySet= New-Object Microsoft.Exchange.WebServices.Data.PropertySet( [Microsoft.Exchange.WebServices.Data.BasePropertySet]::IdOnly)
$Items = $EwsService.FindItems( $RecipientCacheFolder.Id, $ItemView)

It crashes here: $EwsService.FindItems( $RecipientCacheFolder.Id, $ItemView)

With the error:

Exception calling "FindItems" with "2" argument(s): "An internal server error occurred. The operation failed., [FindItem::Execute] itemShape is null. Parameter name: itemShape"

I find zero reference to that error on Internet.

Any ideas?

Thanks

MichelZ commented 1 year ago

$ItemView= New-Object Microsoft.Exchange.WebServices.Data.FolderView( 1000)

Shouldn't this be $ItemView= New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)

(FolderView vs ItemView)

full example courtesy of ChatGPT:


# Set up the EWS service
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1)
$service.Credentials = New-Object System.Net.NetworkCredential("username", "password")
$service.Url = new-object Uri("https://outlook.office365.com/EWS/Exchange.asmx")

# Set up the search criteria
$folderId = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
$itemView = new-object Microsoft.Exchange.WebServices.Data.ItemView(10)
$searchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+ContainsSubstring([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::Subject, "ews")

# Search for items
$findResults = $service.FindItems($folderId, $searchFilter, $itemView)

# Loop through the results and print the subject of each item
foreach ($item in $findResults.Items) {
    Write-Output $item.Subject
}
jrouzies-fr commented 1 year ago

Oh my god you are right ... copy pasted the code from a script and didn't notice this.

Thanks, I'm closing the issue, sorry for that.