Adding Delegates

Topic Last Modified: 2009-01-29

You can use the Microsoft Exchange Web Services (EWS) Managed API to give delegates access to the resources of another account. These delegates can then act on behalf of the account holder to perform tasks for which they are granted permissions.

You can grant delegates to act on behalf of the primary account holder in these areas:

  • Calendars
  • Contacts
  • Inbox
  • Journal
  • Notes
  • Tasks
To add a delegate for an account

  1. Create a delegate object that represents the user who is to be added as a delegate.

    CSharp
    DelegateUser newDelegate = new DelegateUser("delegate@email.address);
  2. Set the permissions that you want to grant to the delegate on the DelegateUser object.

    CSharp
    newDelegate.Permissions.CalendarFolderPermissionLevel = DelegateFolderPermissionLevel.Editor;
  3. Create a list of delegates to add and add the new delegate to the list. This example uses a generic List object to hold the delegate list.

    CSharp
    List<DelegateUser>delegatesToAdd = new List<DelegateUser>();
    delegatesToAdd.Add(newDelegate);
  4. Create a Mailbox object to represent the account that the delegates are being added to.

    Mailbox mailbox = new Mailbox("primaryAccount@email.address");
  5. Call the AddDelegates method to add the new delegate.

    CSharp
    Collection<DelegateUserResponse> result = service.AddDelegates(mailbox, MeetingRequestsDeliveryScope.DelegatesAndMe, delegatesToAdd);

The AddDelegates method adds the specified account holders as delegates of the primary account. The list of account holders can be either an array of DelegateUser objects, or any object that implements the IEnumberable interface and returns DelegateUser objects.

This procedure assumes that a valid ExchangeService object is bound to the primary user's account, or that the primary user's account is being impersonated.

Example

The following code example shows how to add delegates to a primary user by using the AddDelegates method.

ManagedCPlusPlus
    public Collection<DelegateUserResponse> AddDelegates()
    {
      // Bind to the service by using the primary e-mail address credentials.
      ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
      service.Credentials = new NetworkCredential(user1Name, user1Password, emailDomain);
      service.AutodiscoverUrl(user1Email);

      // Create a list to hold the new delegates to add.
      List<DelegateUser> newDelegates = new System.Collections.Generic.List<DelegateUser>();

      // Create a new delegate that has read access to the primary user's calendar.
      DelegateUser calendarDelegate = new DelegateUser(user2Email);
      calendarDelegate.Permissions.CalendarFolderPermissionLevel = DelegateFolderPermissionLevel.Editor;

      // Add the delegate to the list of new delegates.
      newDelegates.Add(calendarDelegate);

      // Create a new delegate that can add tasks to the primary user's task list.
      DelegateUser taskDelegate = new DelegateUser(user3Email);

      // Turn off appointments since this delegate is not for calendar items.
      taskDelegate.ReceiveCopiesOfMeetingMessages = false;

      taskDelegate.Permissions.TasksFolderPermissionLevel = DelegateFolderPermissionLevel.Author;
      taskDelegate.Permissions.InboxFolderPermissionLevel = DelegateFolderPermissionLevel.Editor;
      
      // Add the delegate to the list of new delegates.
      newDelegates.Add(taskDelegate);

      // Create a mailbox object that represents the primary user.
      Mailbox mailbox = new Mailbox(user1Email);

      // Call the AddDelegates method to add the delegates to the primary user's mailbox.
      Collection<DelegateUserResponse> response = service.AddDelegates(mailbox, MeetingRequestsDeliveryScope.DelegatesAndSendInformationToMe, newDelegates);

      return response;
    }

The AddDelegates method in the code example returns a list of DelegateUserResponse objects, one for each delegate that is added to the primary account.

Examine the DelegateUserResponse objects to determine the success or failure for each delegate. If the response contains the ErrorDelegateAlreadyExists enumeration value in the ServiceResult property, use the UpdateDelegate operation to modify the delegate's access to the primary account.

Compiling the Code

For information about compiling this code, see Getting Started.

Robust Programming

  • Properly handle all errors.
  • Check response codes in the method result.
  • Check user input.
  • Use Autodiscover to determine the Web service end point.
Security

  • Use HTTP with SSL for all communications between client and server.
  • Protect user passwords.
  • Validate all user input.
See Also

Tasks

Getting Delegates
Updating Delegates
Removing Delegates

Page view tracker