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...

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.

Export calendar Items to a CSV file using Microsoft Graph and Powershell

For the last couple of years the most constantly popular post by number of views on this blog has been  Export calendar Items to a CSV file using EWS and Powershell closely followed by the contact exports scripts. It goes to show this is just a perennial issue that exists around Mail servers, I think the first VBS script I wrote to do this type of thing was late 90's against Exchange 5.5 using cdo 1.2. Now it's 2020 and if your running Office365 you should really be using the Microsoft Graph API to do this. So what I've done is create a PowerShell Module (and I made it a one file script for those that are more comfortable with that format) that's a port of the EWS script above that is so popular. This script uses the ADAL library for Modern Authentication (which if you grab the library from the PowerShell gallery will come down with the module). Most EWS properties map one to one with the Graph and the Graph actually provides better information on recurrences then...
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.