Friday, December 04, 2009

Using Contact photo's in EWS in Exchange 2010

Contact photo's where a feature first introduced in Outlook 2003 but up until the release on Exchange 2010 not something you could use in EWS. This should not be confused with another new feature in Exchange 2010 which is the ability to upload a picture to the Gal (or the directory to be more precise) if your interested in finding out more about that check out Ilse post .

So the Contact photo's I'm talking about are those that are associated with contact Items in your mailbox. About 4 years ago i posted this script (which was rubbish) for doing this with CDO 1.2 importantly in the heady stile of 2005 i pointed out that when using contact photos you need to set some extended properties on the attachment. Now while you still can't do this with EWS :( in 2010 they have added the ability to set the properties of a contact photo(just wish they hadn't stopped there). Further the EWS Managed API actually makes the process of adding a contact photo quite easy.

So a very simple piece of code to add a contact photo in C# would look like

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
Folder Contacts = Folder.Bind(service, WellKnownFolderName.Contacts);
Contact cn1 = new Contact(service);
cn1.GivenName = "Fred";
cn1.Surname = "Bloggs";
cn1.EmailAddresses[EmailAddressKey.EmailAddress1] = "";
cn1.Subject = "Fred Bloggs";
FileAttachment atattach = cn1.Attachments.AddFileAttachment("c:\\contactphoto.jpg");
atattach.IsContactPhoto = true;

For those looking for a Powershell version that you can put to work I've got one of those as well the following is a script that allows you to specify the emailaddress of the contact you want update and the photo you want to use. The script will then search for this conact in the mailboxes contact folder and upload the picture to it (if found). I've put a download of this script here the code itself looks like.

$MailboxName = ""
$emEmailAdddrestoFind = ""
$cntPhotoFile = "c:\ck1.jpg"

$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.0\Microsoft.Exchange.WebServices.dll"
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010)

$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$sidbind = "LDAP://<SID=" + $windowsIdentity.user.Value.ToString() + ">"
$aceuser = [ADSI]$sidbind


$folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Contacts,$MailboxName)
$ContactsFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
$Iview = new-object Microsoft.Exchange.WebServices.Data.ItemView(1000)
$SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.ContactSchema]::EmailAddress1,$emEmailAdddrestoFind)
$frContactResults = $ContactsFolder.FindItems($SfSearchFilter,$Iview)
foreach ($cnContacts in $frContactResults.Items){
$atattach = $cnContacts.Attachments.AddFileAttachment($cntPhotoFile)
$atattach.IsContactPhoto = $true