Skip to main content

EWS Managed API and Powershell How-To Series Part 5 Delegate Operations

This is part 5 of my ongoing EWS and Powershell How-To series in this part I'm going to be looking at the special EWS operations starting with the Delegate operations in this post.

The Tale of Two Delegates (it is the 200th Anniversary of dickens)

When you talk about delegates with EWS it can be a little confusing because it could be your talking about accessing another mailbox as a delegate for example . For example to access another mailboxes Inbox as a Delegate you would use

  1. $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)     
  2. $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)   
The other type of Delegates are the assigning of Mailbox permissions via the use of the Delegate Operations or to put it pictorially this function in Outlook

Delegates are always the best way of assigning shared rights to users Mailbox and Calendar folders because it always provides accountability to the user eg a user can always go in and see whom their delegates are where as the alternative of assigning rights via Add-MailboxPermission or modifying the folder permission directly may not be a easily visible and accountable to the user.

So first lets look at the code that can be used to get the delegates in a Mailbox

  1. #Get Delegates for Mailbox  
  2. $delegates = $service.getdelegates($MailboxName,$true)  
  3. #Enumerate Delegates  
  4. foreach($Delegate in $delegates.DelegateUserResponses){  
  5.     $Delegate.DelegateUser  
  6. }  
Pretty simple but the response itself is first worth looking at because it contains two distinct data elements, All the information about delegate permissions is held within the DelegateUserResponses collection and the information that controls the Radio buttons under the "Delivery meeting requests addressed to me and responses..." in the above screen shoot is held in a separate property called MeetingRequestsDeliveryScope. This is important when it comes to adding a new delegate.

Adding a New Delegate

  As I've just talked about when you go to add a new delegate you first should request the existing delegates on the Mailbox so you can determine the configuration of the MeetingRequestsDeliveryScope (Otherwise you will risk changing the current configuration) and you can also check if that delegate already exists and just needs to be modified instead. Lets look at a example of adding a delegate to a Mailbox this script will get the existing delegates first check to see if the delegate you want to add already existing and if not adds that delegate and make sure this delegate has Reviewer rights to the Inbox.

  1. $delegatetoAdd = "bob.test@domain.com"  
  2. $exists = $false  
  3.   
  4. $delegates = $service.getdelegates($MailboxName,$true)  
  5. foreach($Delegate in $delegates.DelegateUserResponses){  
  6.     $Delegate.DelegateUser.UserId.PrimarySmtpAddress  
  7.     if($Delegate.DelegateUser.UserId.PrimarySmtpAddress.ToLower() -eq $delegatetoAdd.ToLower()){  
  8.         #Change Inbox permissions  
  9.         $Delegate.DelegateUser.Permissions.InboxFolderPermissionLevel = [Microsoft.Exchange.WebServices.Data.DelegateFolderPermissionLevel]::Reviewer  
  10.         $service.UpdateDelegates($MailboxName,$delegates.MeetingRequestsDeliveryScope,$Delegate.DelegateUser)  
  11.         $exists = $true  
  12.         "Delegate Updated"  
  13.     }  
  14. }   
  15. if($exists -eq $false){  
  16.     $dgUser = new-object Microsoft.Exchange.WebServices.Data.DelegateUser($delegatetoAdd)  
  17.     $dgUser.ViewPrivateItems = $false  
  18.     $dgUser.ReceiveCopiesOfMeetingMessages = $false  
  19.     $dgUser.Permissions.CalendarFolderPermissionLevel = [Microsoft.Exchange.WebServices.Data.DelegateFolderPermissionLevel]::Editor  
  20.     $dgUser.Permissions.InboxFolderPermissionLevel = [Microsoft.Exchange.WebServices.Data.DelegateFolderPermissionLevel]::Reviewer  
  21.     $dgArray = new-object Microsoft.Exchange.WebServices.Data.DelegateUser[] 1  
  22.     $dgArray[0] = $dgUser  
  23.     $service.AddDelegates($MailboxName,$delegates.MeetingRequestsDeliveryScope,$dgArray)  
  24.     "Delegate Added"  
  25. }  

Removing a Delegate

To remove a delegate its pretty similar to adding a new one you should first get the existing delegates then use the removedelegates method eg 

  1. $delegateToRemove = "bob.test@domain.com"  
  2. $delegates = $service.getdelegates($MailboxName,$true)  
  3. foreach($Delegate in $delegates.DelegateUserResponses){  
  4.     $Delegate.DelegateUser.UserId.PrimarySmtpAddress  
  5.     if($Delegate.DelegateUser.UserId.PrimarySmtpAddress.ToLower() -eq $delegatetoAdd.ToLower()){  
  6.         $service.RemoveDelegates($MailboxName,$delegateToRemove)  
  7.         "Removing Delegate"  
  8.     }  
  9. }  
Reporting on Delegates

One of the more useful things to do with delegates is produce a report of the currently configured delegates on a mailbox . This can come in use if you want to send a report to a user whom may have no idea who currently has access to their mailbox. Here's a sample of producing a HTML report of the delegates on a mailbox.

  1. $rptCollection = @()  
  2. $delegates = $service.getdelegates($MailboxName,$true)  
  3. foreach($Delegate in $delegates.DelegateUserResponses){  
  4.     $rptObj = "" | select EmailAddress,Inbox,Calendar,Contacts,Tasks,Notes,Journal,MeetingMessages,ViewPrivateItems  
  5.     $rptObj.EmailAddress = $Delegate.DelegateUser.UserId.PrimarySmtpAddress  
  6.     $rptObj.Inbox = $Delegate.DelegateUser.Permissions.InboxFolderPermissionLevel  
  7.     $rptObj.Calendar = $Delegate.DelegateUser.Permissions.CalendarFolderPermissionLevel  
  8.     $rptObj.Contacts = $Delegate.DelegateUser.Permissions.ContactsFolderPermissionLevel  
  9.     $rptObj.Tasks = $Delegate.DelegateUser.Permissions.TasksFolderPermissionLevel  
  10.     $rptObj.Notes = $Delegate.DelegateUser.Permissions.NotesFolderPermissionLevel  
  11.     $rptObj.Journal = $Delegate.DelegateUser.Permissions.JournalFolderPermissionLevel  
  12.     $rptObj.ViewPrivateItems = $Delegate.DelegateUser.ViewPrivateItems  
  13.     $rptObj.MeetingMessages = $Delegate.DelegateUser.ReceiveCopiesOfMeetingMessages  
  14.     $rptCollection += $rptObj  
  15. }  
  16.   
  17. $tableStyle = @" 
  18. <style> 
  19. BODY{background-color:white;} 
  20. TABLE{border-width: 1px; 
  21.   border-style: solid; 
  22.   border-color: black; 
  23.   border-collapse: collapse; 
  24. } 
  25. TH{border-width: 1px; 
  26.   padding: 10px; 
  27.   border-style: solid; 
  28.   border-color: black; 
  29.   background-color:#66CCCC 
  30. } 
  31. TD{border-width: 1px; 
  32.   padding: 2px; 
  33.   border-style: solid; 
  34.   border-color: black; 
  35.   background-color:white 
  36. } 
  37. </style> 
  38. "@  
  39.     
  40. $body = @" 
  41. <p style="font-size:25px;family:calibri;color:#ff9100">  
  42. $TableHeader  
  43. </p>  
  44. "@  
  45.   
  46. $rptCollection | ConvertTo-HTML -head $tableStyle | Out-File c:\delgateReport.html  
When it all goes wrong

Delegates are one of the things for a number of reasons that can become corrupted or Invalid. The Delegate operations that the  EWS Managed API provides abstracts of the Raw Delegate information which is held in number of properties on the Local Freebusy information the Folder DACL and possibly a Inbox Rule if calendar forwards are enabled. To Access and fix this raw data if something isn't working have a look at this post 

Popular posts from this blog

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Gr...

EWS-FAI Module for browsing and updating Exchange Folder Associated Items from PowerShell

Folder Associated Items are hidden Items in Exchange Mailbox folders that are commonly used to hold configuration settings for various Mailbox Clients and services that use Mailboxes. Some common examples of FAI's are Categories,OWA Signatures and WorkHours there is some more detailed documentation in the https://msdn.microsoft.com/en-us/library/cc463899(v=exchg.80).aspx protocol document. In EWS these configuration items can be accessed via the UserConfiguration operation https://msdn.microsoft.com/en-us/library/office/dd899439(v=exchg.150).aspx which will give you access to either the RoamingDictionary, XMLStream or BinaryStream data properties that holds the configuration depending on what type of FAI data is being stored. I've written a number of scripts over the years that target particular FAI's (eg this one that reads the workhours  http://gsexdev.blogspot.com.au/2015/11/finding-timezone-being-used-in-mailbox.html is a good example ) but I didn't have a gene...

Sending a MimeMessage via the Microsoft Graph using the Graph SDK, MimeKit and MSAL

One of the new features added to the Microsoft Graph recently was the ability to create and send Mime Messages (you have been able to get Message as Mime for a while). This is useful in a number of different scenarios especially when trying to create a Message with inline Images which has historically been hard to do with both the Graph and EWS (if you don't use MIME). It also opens up using SMIME for encryption and a more easy migration path for sending using SMTP in some apps. MimeKit is a great open source library for parsing and creating MIME messages so it offers a really easy solution for tackling this issue. The current documentation on Send message via MIME lacks any real sample so I've put together a quick console app that use MSAL, MIME kit and the Graph SDK to send a Message via MIME. As the current Graph SDK also doesn't support sending via MIME either there is a workaround for this in the future my guess is this will be supported.
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.