Install ADFS on Azure VM step by step

Getting Contacts from user mailbox using EWS managed API

In my previous posts on EWS we worked on how to move, remove, export emails items, in this post I will demonstrate how we can work with user Contacts and export the contact in CSV or fetch the contact details.
If you are following my previous post then you would know we just need to access the user Contacts folder and load the items in it with the property set and then get the data we need.

$ContactsFolderid= new-object Microsoft.Exchange.WebServices.Data.FolderId `
([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Contacts,$Mailbox)
$ContactsFolder=[Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$ContactsFolderid)
$items = $service.FindItems($ContactsFolder.ID,$view)
$service.LoadPropertiesForItems($items, [Microsoft.Exchange.WebServices.Data.PropertySet]::FirstClassProperties)

Most of the data is easily available when it comes to contacts but there are few attributes where we needs to go into the little deep.
So let me talk about how do we work with these attribute and get the data from each member.
Below is the snap shot of a single contact.
You can get it from collection using below method and then check what all field you need and then add the same into the table.
$items.Items[0]



So lets see how we get the data from the attribute with Dictionary object.
If you do “$items.Items[0] | get-member” on any dictionary object you will be able to see the method and ParameterizedProperty.











The best way to get the member for the item is just put the highlighted string in google and check the msdn link for detailed member list.
Like below link for IMAddress

Now you know the member key name we can fetch the data from the member.
$items.items[0].ImAddresses[[Microsoft.Exchange.WebServices.Data.ImAddressKey]::ImAddress1]

The same way you can fetch data from other attributes, below I have put a PowerShell script for full code review, I hope you find this post informative, your feedback and suggestions are welcome in comment section.

param (
$mailbox="sunil.chauhan@xyz.com",
$itemsView=1000,
$userName=$cred.UserName,
$password=$cred.GetNetworkCredential().password
)

$uri=[system.URI] "https://outlook.office365.com/ews/exchange.asmx"
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
Import-Module $dllpath

## Set Exchange Version
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
$service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $userName, $password
$service.url = $uri
$service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId `
([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SMTPAddress,$Mailbox);

$ContactsFolderid= new-object Microsoft.Exchange.WebServices.Data.FolderId `
([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Contacts,$Mailbox)
$ContactsFolder=[Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$ContactsFolderid)
$view = New-Object Microsoft.Exchange.WebServices.Data.ItemView -ArgumentList $ItemsView
$items = $service.FindItems($ContactsFolder.ID,$view)

[void]$service.LoadPropertiesForItems($items, [Microsoft.Exchange.WebServices.Data.PropertySet]::FirstClassProperties)
$allContacts=@()

foreach ($item in $items.items) {

$cValues = "" | Select FileAs,DisplayName,GivenName,Surname,CompanyName,EmailAddresses,OfficeLocation,PhysicalAddresses, `
PhoneNumbers,Department,JobTitle,ImAddress

$cValues.FileAS = $item.FileAs
$cValues.DisplayName = $item.DisplayName
$cValues.GivenName = $item.GivenName
$cValues.Surname = $item.Surname
$cValues.CompanyName = $item.CompanyName
$cValues.EmailAddresses = $item.EmailAddresses[[Microsoft.Exchange.WebServices.Data.EmailAddressKey]::EmailAddress1].Address
$cValues.OfficeLocation = $item.OfficeLocation
$cValues.PhysicalAddresses = $item.PhysicalAddresses[[Microsoft.Exchange.WebServices.Data.PhysicalAddressKey]::Business].City
$cValues.PhoneNumbers = $item.PhoneNumbers[[Microsoft.Exchange.WebServices.Data.PhoneNumberKey]::MobilePhone]
$cValues.Department = $item.Department
$cValues.JobTitle = $item.JobTitle
$cValues.ImAddress = $item.ImAddresses[[Microsoft.Exchange.WebServices.Data.ImAddressKey]::ImAddress1]
$allContacts+=$cValues
}
  
$allContacts

Comments