Sunday, March 04, 2007

Pull Subscriptions using Exchange 2007 Web Service

Here is the complete code for finding created event and new mail event using webservice

'Create the binding and set the credentials.
Dim esb As New ExchangeServiceBinding()
esb.Url = "http://www.domain.com/EWS/exchange.asmx"
esb.Credentials = New NetworkCredential("administrator", "password", "domain")



' Create a new Subscribe request.
Dim subscribeRequest As New SubscribeType()
Dim pullSubscription As New PullSubscriptionRequestType()

' Identify the folders monitored for events.
Dim folders(2) As BaseFolderIdType
Dim folderId As New DistinguishedFolderIdType
folderId.Id = DistinguishedFolderIdNameType.inbox
folders(0) = folderId

Dim folderId1 As New DistinguishedFolderIdType
folderId1.Id = DistinguishedFolderIdNameType.calendar
folders(1) = folderId1

pullSubscription.FolderIds = folders

' Identify the events monitored for the subscription.
Dim eventTypes(2) As NotificationEventTypeType
eventTypes(0) = NotificationEventTypeType.NewMailEvent
eventTypes(1) = NotificationEventTypeType.CreatedEvent
pullSubscription.EventTypes = eventTypes

' Define the timeout period for the subscription.
pullSubscription.Timeout = 10

subscribeRequest.Item = pullSubscription

' Send the Subscribe request and receive the Subscribe response.
Dim subscribeResponse As SubscribeResponseType = esb.Subscribe(subscribeRequest)

' Check the results.
If (subscribeResponse.ResponseMessages.Items.Length > 0 And subscribeResponse.ResponseMessages.Items(0).ResponseClass = ResponseClassType.Success) Then

Dim subscribeResponseMessage As SubscribeResponseMessageType = subscribeResponse.ResponseMessages.Items(0)
Response.Write("Subscribed for Pull notifications: " & subscribeResponseMessage.SubscriptionId)

' Wait 30 seconds before receiving event notifications.
'Thread.Sleep(30000)

' Create a new GetEvents request.
Dim getEventsRequest As New GetEventsType()

' Identify the subscription identifier and watermark for the subscription
' that will be polled for changes in the Exchange store.
getEventsRequest.SubscriptionId = subscribeResponseMessage.SubscriptionId
getEventsRequest.Watermark = subscribeResponseMessage.Watermark

' Send the GetEvents request and receive the GetEvents response.
Dim eventsResponse As GetEventsResponseType = esb.GetEvents(getEventsRequest)

' Check the results.
If (eventsResponse.ResponseMessages.Items.Length > 0 And eventsResponse.ResponseMessages.Items(0).ResponseClass = ResponseClassType.Success) Then
Dim eventsResponseMessage As GetEventsResponseMessageType = eventsResponse.ResponseMessages.Items(0)
For Each type As ItemsChoiceType In eventsResponseMessage.Notification.ItemsElementName
If type = ItemsChoiceType.NewMailEvent Then
Dim notification As NotificationType
notification = eventsResponseMessage.Notification
Dim eventChangedTye As BaseObjectChangedEventType
eventChangedTye = notification.Items(0)
Dim MailItemIdType As ItemIdType
MailItemIdType = eventChangedTye.Item
Response.Write(MailItemIdType.Id)

End If
Next
End If

' Create an Unsubscribe request.
Dim unsubscribeRequest As New UnsubscribeType()

' Identify the subscription to unsubscribe from.
unsubscribeRequest.SubscriptionId = subscribeResponseMessage.SubscriptionId

' Send the Unsubscribe request and receive the Unsubscribe response.
Dim unsubscribeResponse As UnsubscribeResponseType = esb.Unsubscribe(unsubscribeRequest)

' Check the results
If (unsubscribeResponse.ResponseMessages.Items.Length > 0 And unsubscribeResponse.ResponseMessages.Items(0).ResponseClass = ResponseClassType.Success) Then
Response.Write("Subscribtionunsubscribed successfully: " & unsubscribeRequest.SubscriptionId)
End If
End If




2 comments:

Anonymous said...

Hi Anuparu

Did you ever experience "403" Forbidden" responce for the Subscribe request?

My other ews calls work just fine, but the Subscribe one returns 403

Anu said...

Make sure your client access server is able to accept subscription request. Can be a problem with setting domain name too