How to: Pull notifications about mailbox events by using EWS in Exchange

Exchange

Find out how to use the EWS Managed API or EWS to subscribe to pull notifications and get events.

Last modified: March 09, 2015

Applies to: Exchange Online | Exchange Server 2013 | Office 365

EWS in Exchange uses pull notifications to enable clients to request (or pull) notifications about changes to the mailbox from the server to the client.

If you’re subscribing to pull notifications by using the EWS Managed API, you subscribe to and get pull notifications by using the SubscribeToPullNotifications method. You then get events from the server by using the GetEvents method.

To subscribe to pull notifications by using EWS, you create a subscription by using the Subscribe operation, parse the response, and then get the notifications by using the GetEvents operation.

After the client receives notifications of items that are changed or created on the server, it can then synchronize the changes.

The following code example shows how to use the SubscribeToPullNotifications method to subscribe to pull notifications for all events in the Inbox folder. The example then uses the GetEvents method to retrieve events from the server. In this example, we assume that service is a valid ExchangeService binding.

// Subscribe to pull notifications in the Inbox.
PullSubscription subscription = service.SubscribeToPullNotifications( 
    new FolderId[] { WellKnownFolderName.Inbox }, 30, null, 
    EventType.NewMail, EventType.Created, EventType.Deleted,
    EventType.Modified, EventType.Moved, EventType.Copied, EventType.FreeBusyChanged); 
 
// Call GetEvents to retrieve events from the server. 
GetEventsResults events = subscription.GetEvents(); 

After you receive an event from the server, you can synchronize those changes with the server. Use one of the unsubscribe methods specified in How do I unsubscribe to notifications? to end the subscription with the server when the subscription is no longer needed.

The following example shows the XML request to send to the server to subscribe to all EventTypes in the Inbox folder by using the Subscribe operation. This is also the XML request that the EWS Managed API sends when subscribing to pull notifications by using the SubscribeToPullNotifications method.

<?xml version="1.0" encoding="utf-8"?>
<Subscribe xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <PullSubscriptionRequest xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
    <FolderIds xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
      <DistinguishedFolderId Id="inbox" />
    </FolderIds>
    <EventTypes xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
            <EventType>NewMailEvent</EventType>
            <EventType>CreatedEvent</EventType>
            <EventType>DeletedEvent</EventType>
            <EventType>ModifiedEvent</EventType>
            <EventType>MovedEvent</EventType>
            <EventType>CopiedEvent</EventType>
            <EventType>FreeBusyChangedEvent</EventType>
    </EventTypes>
    <Timeout xmlns="http://schemas.microsoft.com/exchange/services/2006/types">30</Timeout>
  </PullSubscriptionRequest>
</Subscribe>

The following XML example shows the SubscribeResponse message that is sent from the server to the client in response to the Subscribe operation request. The inclusion of the NoError value for the ResponseCode element means that the subscription was created successfully. The SubscriptionId element uniquely identifies the pull notification subscription on the server. The Watermark element represents a bookmark in the mailbox event queue.

<?xml version="1.0" encoding="utf-8"?>
<SubscribeResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ResponseMessages xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
    <SubscribeResponseMessage ResponseClass="Success">
      <ResponseCode>NoError</ResponseCode>
      <SubscriptionId>d581ab79-a2ec-4653-9c8e-564d7cfc1d8c</SubscriptionId>
      <Watermark>AAAAAGUhAAAAAAAAAQ==</Watermark>
    </SubscribeResponseMessage>
  </ResponseMessages>
</SubscribeResponse>

After creating the subscription, you can now get events by using the SubscriptionId that is returned in the SubscribeResponse message.

The following XML example shows the GetEvents operation request message that is sent from the client to the server to get notifications for the SubscriptionId that is returned in the SubscribeResponse message. For the first GetEvents request, use the Watermark returned in the Subscribe response. For subsequent GetEvents requests, use the last Watermark that was returned in the previous GetEvents request.

<?xml version="1.0" encoding="utf-8"?>
<GetEvents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <SubscriptionId xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">d581ab79-a2ec-4653-9c8e-564d7cfc1d8c</SubscriptionId>
  <Watermark xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">AAAAAGUhAAAAAAAAAQ==</Watermark>
</GetEvents>

The following XML example shows the GetEvents response message that is sent from the server to the client. Each GetEvents response includes information about one or more events. A Watermark is returned for each event. The last Watermark must be saved and used in the next GetEvents request. If no store events have occurred since the last GetEvents request, a status event is returned.

<?xml version="1.0" encoding="utf-8"?>
<GetEventsResponseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                       xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ResponseMessages xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">
    <GetEventsResponseMessage ResponseClass="Success">
      <ResponseCode>NoError</ResponseCode>
      <Notification>
        <SubscriptionId xmlns="http://schemas.microsoft.com/exchange/services/2006/types">d581ab79-a2ec-4653-9c8e-564d7cfc1d8c</SubscriptionId>
        <PreviousWatermark xmlns="http://schemas.microsoft.com/exchange/services/2006/types">AAAAAGUhAAAAAAAAAQ==</PreviousWatermark>
        <MoreEvents xmlns="http://schemas.microsoft.com/exchange/services/2006/types">false</MoreEvents>
        <NewMailEvent xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
          <Watermark>AAAAAHMhAAAAAAAAAQ==</Watermark>
          <TimeStamp>2013-09-15T21:37:01Z</TimeStamp>
          <ItemId Id="AAAtA=" ChangeKey="CQAAAA==" />
          <ParentFolderId Id="AQAtAEFkbWA==" ChangeKey="AQAAAA==" />
        </NewMailEvent>
      </Notification>
    </GetEventsResponseMessage>
  </ResponseMessages>
</GetEventsResponse>

After you receive an event from the server, synchronize the changes to the client. Use the Unsubscribe operation to end the subscription with the server when the subscription is no longer needed.

Show:
© 2015 Microsoft