5 Lesser Known Operations in Exchange Web Services on Exchange 2013

Editor’s note: The following post was written by Exchange Server MVP Glen Scales

 5 new lesser known operations in Exchange Web Services on Exchange 2013 and how to use them


In this blog post I’m going to look at 5 of the lesser known new Exchange Web Services Operations in Exchange 2013. As with most of the new features in 2013 these new operations build on top of the EWS foundation from 2007 and 2010 and allows the easier programmatic manipulation of the Mailbox data.

1.     Getting the User Photo from Exchange via EWS

EWS in 2013 now has two methods you can use to Get User photos, there is a normal SOAP based EWS operation called GetUserPhoto and there is also a REST-based end point. The REST based operation can be useful for non EWS applications where you just want to make use of the user Photo. To use this REST-based end point to access the photo you just need the SMTP Address of the user and the size of the user photo you want. The sizes that can requested are

Size Code



If you request HR48x48 then the Active Directory thumbnail is returned

















If you request a size that is not available the largest available size will be returned if no picture is available the active directory thumbnail gets returned.

Managed API Sample – While there are no methods in the EWS Managed API to access the GetUserPhoto operation you can still use the Autodiscover Class to find the ExternalPhotoURL and then use any of the regualr.NET Web Request classes such as the WebClient Class to access the REST endpoint. The following is a PowerShell sample that shows how to do this






Add-Type-Path“C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll”



       param (

              $EmailAddress=“$( throw ‘Email is a mandatory Parameter’ )”,

                     $Credentials=“$( throw ‘Credentials is a mandatory Parameter’ )”



              $version= [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013



              #This Example does an Autodiscover against Exchange Online

             $uri=[system.URI] (“https://autodiscover-


              $adService.Url =$uri 






              $adService.RedirectionUrlValidationCallback= {$true}



Microsoft.Exchange.WebServices.Autodiscover.UserSettingName[] 1

              $UserSettings[0] =


              $adResponse=$adService.GetUserSettings($MailboxName, $UserSettings)







#Example Use

$pscreds= (Get-Credential)

$PhotoURL=AutoDiscoverPhotoURL-EmailAddress$MailboxName -Credentials $pscreds




#Download photo to a file


#Get photo as a ByteArray



2.     – How to Archive an Item using the ArchiveItem  Operation in EWS

Personal Archives is a feature that was introduced in Exchange 2010 and again improved on in Exchange 2013. The ArchiveItem Operation is aimed at giving you the ability to move items from your Primary Mailbox to your Personal Archive. For example this operation can be used to selectively Archive Messages from the Inbox folder based on a particular Search criteria. The following sample will Archive messages that are older than 1 year to the Archive Store while maintaining the folder hierarchy.


$queryTime= (Get-Date).AddYears(1)

$AQSString= “System.Message.DateReceived:<“+$queryTime.ToString(“MM/dd/yyyy”)  

# Bind to the Inbox Folder



$ivItemView=  New-ObjectMicrosoft.Exchange.WebServices.Data.ItemView(1000)   




$archiveItems= [Activator]::CreateInstance($type) 










$type= (“System.Collections.Generic.List”+‘`’+“1”) -as“Type”


$batchArchive= [Activator]::CreateInstance($type)



      if($batchArchive.Count-eq 100){






if($batchArchive.Count-gt 0){






3.     Mark all the messages in a folder Read using the MarkAllAsRead Operation in EWS

  Everybody knows the pain of having to mark a lot of messages read after coming back from a holiday. This new operation in EWS makes it easy to do this for all messages within a particular folder (In previous versions you would have had to find the messages that where unread and then modify each message individually). In the EWS Managed API to use this operation you can take advantage of two new methods that have been added to the folder class MarkAllItemsAsRead and MarkAllItemsAsUnread. For example here is how you can mark all the Unread Messages in the Inbox folder as Read (the Boolean parameter in the method tells Exchange whether to suppress Read Recipients or not).



  # Bind to the Inbox Folder



“Number of Messages Unread : “+$Inbox.UnreadCount



4.     Marking messages as Junk Email

Another new Operation that has been added to EWS is the ability to Mark and UnMark messages as JunkEmail and move these messages to and from the Junk Email folder. This Operation does two separate things when you use it

·         Moves a messages To or From the Junk Email Folder

·         Add or Removes the sender of the Messages you’re marking or unmarking as Junk Email to the blocked sender list of that Mailbox.

To use this operation you first need to get the ItemIds of the Items you want to Mark or UnMark as Junk Email. This is typically done in EWS using the FindItem Operation and then filtering on a particular Item property. To control what the MarkAsJunk operation does in the EWS Managed API the MarkAsJunk method has two parameters IsJunk and MoveItem. The following table lists the combinations and actions for these parameters








Item will be moved to the Junk email folder and email sender added to Blocked Senders List of the Mailbox

Inbox /Any



The Sender of the Email is added to the Blocked Senders List




Item will be moved to the Inbox folder and email Sender will be removed from the Blocked Senders List of the Mailbox




The Sender of the Email will be removed from the Blocked Senders list of the Mailbox


In this example Messages that have NewLetter in the Subject are marked as JunkEmail and moved to the JunkEmail Folder using the MarkAsJunk operation.


# Bind to the Inbox Folder



#Define ItemView to retrive just 1000 Items   

$ivItemView=  New-ObjectMicrosoft.Exchange.WebServices.Data.ItemView(1000)   


$type= (“System.Collections.Generic.List”+‘`’+“1”) -as“Type”


$JunkItems= [Activator]::CreateInstance($type) 

















5.     Using UserRetentionPolicyTags

While it is possible in Exchange 2010 to use UserRetentionPolicyTags in EWS it did require that you define and use the raw extended properties. In 2013 in the EWS using the UserRetentionPolicyTags is made easier through the integration of these tags into the FolderClass and also the GetUserRetentionPolicyTags operation which can be used to get all the user RetentionPolicyTags and their associated settings. For example here is how to create a new folder and set the RetentionPolicyTag on that Folder to a UserRetentionPolicyTag called “1 Month Delete”.




foreach ($rtTagin$getRTResp.RetentionPolicyTags) {

    if ($rtTag.DisplayName-eq“1 Month Delete”)



              $NewFolder.DisplayName=“My New Folder12345” 









About the author


Glen Scales is a freelance software developer and engineer who specializes in Microsoft Exchange APIs used to create customized solutions for a range of clients and industries, particularly in the realm of cloud-based services within messaging environments. He has been an Exchange MVP since 2004, contributing regularly to the Exchange development forums on TechNet and creating and sharing open source code libraries and scripts on his blog (http://gsexdev.blogspot.com) and twitter @glenscales.

About MVP Mondays

The MVP Monday Series is created by Melissa Travers. In this series we work to provide readers with a guest post from an MVP every Monday. Melissa is a Community Program Manager, formerly known as MVP Lead, for Messaging and Collaboration (Exchange, Lync, Office 365 and SharePoint) and Microsoft Dynamics in the US. She began her career at Microsoft as an Exchange Support Engineer and has been working with the technical community in some capacity for almost a decade. In her spare time she enjoys going to the gym, shopping for handbags, watching period and fantasy dramas, and spending time with her children and miniature Dachshund. Melissa lives in North Carolina and works out of the Microsoft Charlotte office.

Comments (2)

  1. vnus.ht@hotmail.co.th says:

    my like good.

  2. Chris128 says:

    You mentioned how to get the user photos from Exchange 2013, but do you know if it is possible to set them via EWS as well? Specifically I'm looking to do it via the EWS Managed API (as I'm a .NET developer) but can't find much documentation on actually setting the photo without using the Powershell commands Set-UserPhoto (which are no use to me because my application should be able to run on a machine that doesn't have any Exchange tools installed on it)



Skip to main content