Share via


Adding Delegates (Exchange Web Services)

Topic Last Modified: 2008-09-03

Starting with Microsoft Exchange Server 2007 Service Pack 1 (SP1), you can use Exchange Web Services to add, update, and remove delegates.

This topic contains a code example that shows how to add delegates that can take actions on behalf of a principal.

Example

The following code example shows how to add two delegate users to an account. In this example, user1 makes the AddDelegate call to add user2 and user3 as delegates. User2 is given Reviewer permissions on user1's calendar. User3 is given Author permissions on the Tasks folder and Editor permissions on the Inbox folder for user1.

static void AddDelegate()
{
    // Set the version, credentials, and Client Access server on ExchangeServiceBinding.
    ExchangeServiceBinding esb = new ExchangeServiceBinding();
    esb.RequestServerVersionValue = new RequestServerVersion();
    esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007_SP1;
    esb.Credentials = new NetworkCredential("username", "password", "domain");
    esb.Url = "https://example.com/ews/exchange.asmx";

    // Create the request.
    AddDelegateType request = new AddDelegateType();

    // Identify the principal's mailbox.
    request.Mailbox = new EmailAddressType();
    request.Mailbox.EmailAddress = "user1@example.com";

    // Identify the delegates who are given access to the principal's mailbox.
    request.DelegateUsers = new DelegateUserType[2];
    request.DelegateUsers[0] = new DelegateUserType();
    request.DelegateUsers[0].UserId = new UserIdType();
    request.DelegateUsers[0].UserId.PrimarySmtpAddress = "user2@example.com";
    request.DelegateUsers[1] = new DelegateUserType();
    request.DelegateUsers[1].UserId = new UserIdType();
    request.DelegateUsers[1].UserId.PrimarySmtpAddress = "user3@example.com";
    
    // Specify the permissions that are granted to each delegate.
    request.DelegateUsers[0].DelegatePermissions = new DelegatePermissionsType();
    request.DelegateUsers[0].DelegatePermissions.CalendarFolderPermissionLevel = DelegateFolderPermissionLevelType.Reviewer;
    request.DelegateUsers[0].DelegatePermissions.CalendarFolderPermissionLevelSpecified = true;
    request.DelegateUsers[1].DelegatePermissions = new DelegatePermissionsType();
    request.DelegateUsers[1].DelegatePermissions.TasksFolderPermissionLevel = DelegateFolderPermissionLevelType.Author;
    request.DelegateUsers[1].DelegatePermissions.TasksFolderPermissionLevelSpecified = true;
    request.DelegateUsers[1].DelegatePermissions.InboxFolderPermissionLevel = DelegateFolderPermissionLevelType.Editor;
    request.DelegateUsers[1].DelegatePermissions.InboxFolderPermissionLevelSpecified = true;

    // Specify whether the principal recieves meeting requests.
    request.DeliverMeetingRequests = DeliverMeetingRequestsType.DelegatesAndSendInformationToMe;
    request.DeliverMeetingRequestsSpecified = true;

    try
    {
        // Send the request and get the response.
        AddDelegateResponseMessageType response = esb.AddDelegate(request);
        DelegateUserResponseMessageType[] responseMessages = response.ResponseMessages;

        // One DelegateUserResponseMessageType exists for each attempt to add a delegate user to an account.
        foreach (DelegateUserResponseMessageType user in responseMessages)
        {
            Console.WriteLine("Results of adding user: " + user.ResponseClass.ToString());
            Console.WriteLine(user.DelegateUser.UserId.DisplayName);
            Console.WriteLine(user.DelegateUser.UserId.PrimarySmtpAddress);
            Console.WriteLine(user.DelegateUser.UserId.SID);
            if (user.DelegateUser.ReceiveCopiesOfMeetingMessagesSpecified)
            {
                Console.WriteLine("Does the delegate receive copies of meeting related messages: " + user.DelegateUser.ReceiveCopiesOfMeetingMessages);
            }
            if (user.DelegateUser.ViewPrivateItemsSpecified)
            {
                Console.WriteLine("Can the delegate view private items in the principal's mailbox: " + user.DelegateUser.ViewPrivateItems);
            }
        }
        Console.ReadLine();
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        Console.ReadLine();
    }
}

Note the following when you are adding delegates:

  • The version header must be set to Exchange2007_SP1 for this example to work.
  • One response message is sent for each delegate user who is added to an account.
  • The client can set delegate settings for multiple delegates in a single request.
  • If the delegate user is already a delegate of the principal, the ErrorDelegateUserAlreadyExists error response code will be returned. If this occurs, use UpdateDelegate instead of AddDelegate.