How to get response of exchange push notification in c#


  • I am using exchange managed API and using push notification in C#. I am using below code when register URL

    Uri uri = new Uri("http://domain/MyPage.aspx");

    PushSubscription ps = service.SubscribeToPushNotifications(folder, uri, 1, "", EventType.Created, EventType.Modified, EventType.Deleted);

    Now i get a hit on domain/MyPage.aspx when i change a event from calendar.

    But now how i process that response ? There is limited value in request header. how could i know that which calendar, which service this request come. How i fetch values from this URL hit.

    I need C# code to get all information when exchange hit my web page.

    Monday, March 06, 2017 9:35 AM

All replies

  • When you created the Subscription you would have received a subscription Id, you need to save that information somewhere and then use that to correlate that back to Mailbox/Folder when you receive the Push Response which will contain the SubscriptionId that the Push belongs to.


    Tuesday, March 07, 2017 3:29 AM
  • Yes I know this and I am getting subscription Id successfully.

    But my question is that how I fetch these value when I receive the Push Response. From where I get these information.

    I am developing web application in C#, I checked these value in Request.header, but nothing is there.

    Glen, Please help me. I am stuck here.

    Tuesday, March 07, 2017 5:54 AM
  • That information isn't in the Push response that why you need to save this information when you created the Subscription eg I create a Subscription for the Inbox for and I receive a SubscriptionId in response I then need to save this information so the endpoint that receives the push knows what Mailbox the Push request with SubscriptionId belongs to. So when your service gets the Push it will have the SubscriptionId and ItemId of the Item that changed etc. That response wont tell you what mailbox it belongs to so its up to you service to work it out based on the SubscriptionId.


    Tuesday, March 07, 2017 10:40 PM
  • Hi Glen,

    I save my SubscriptionId when i create Subscription.

    Now as you says when my service gets the Push it will have the SubscriptionId and ItemId of the Item that changed etc. 

    But my question is that from where I get SubscriptionId and ItemId in response ?

    How I read response ?  I am unable to read response.

    Wednesday, March 08, 2017 9:32 AM
  • Sorry I don't understand your question eg

    >>I save my SubscriptionId when i create Subscription.

    Okay with you here you create a subscription and you save the Subscription Id and FolderId and Mailbox you made the subscription on ?

    >>Now as you says when my service gets the Push it will have the SubscriptionId and ItemId of the Item that changed etc. 

    Okay so the Push request will contain the SubscriptionId and ItemId >> But my question is that from where I get SubscriptionId and ItemId in response ? How I read response ?

    What response are you talking about ? can you post and example response ? you will get Push request from the server with the subscriptionId and ItemId when a change occurs, you won't get anything else other then this (apart from heartbeat). If you want information on the Item that caused the notification you will need to make a GetItem request for the ItemId that caused the Push. If you have multiple users and need to impersonate the owner of the Mailbox where this item is located this is when you need to use the Subscriptionid you saved before to work out what Mailbox this push notification belongs to so you can then set the impersonation header correctly etc.

    Thursday, March 09, 2017 2:53 AM
  • Hi Glen,

    Let me clear my question.

    >> I save my SubscriptionId when i create Subscription. I pass my web URL suppose when i create subscription as below

    service.SubscribeToPushNotifications(folder, "", 1, "", EventType.Created);

    Now I Get the Push request on when a event is created on calendar.

    Now my question arise :

    In Page ExchangeNotification.aspx, where is my SubscriptionId and ItemId. How I can fetch these value ?

    This is my C# ExchangeNotification.aspx page code


        protected void Page_Load(object sender, EventArgs e)

    //  Where is my SubscriptionId and ItemId 

    // Request.Header ,,,,    there is no  SubscriptionId and ItemId  in request


    Thursday, March 09, 2017 12:30 PM
  • A Push notification will be sent to your end point as a SendNotification and you endpoint needs to deal with processing it (eg parsing the SOAP Message). It a SOAP message like all EWS request but its posted from the Exchange server to your endpoint so your code needs to deal with it there are a number of sample listeners on .

    Friday, March 10, 2017 12:54 AM
  • Hi Glen,

    Examples which you provide me, is not fully helpful. these are window application where we need to give our ipaddress and port to receive push notification. which is not to easy.

    But, I got this.

    I found it in Request.InputStream

    StreamReader stream = new StreamReader(Request.InputStream);
                string x = stream.ReadToEnd();
                string xml = HttpUtility.UrlDecode(x);

    here in XML string, i found all information.

    Glen, i have further one more question.

    Now, how I send to OK response to exchange api OR Unscribe response to exchange ?

    Wednesday, March 15, 2017 11:42 AM
  • Your listener needs to respond to the POST from the server with the SendNotificationResultType SOAP message if you look at the way the samples does it or you will need to implement something simular. To unsubscribe you need to respond with Unsubscribe in the Heatbeat instead of okay else it will disappear itself after 3 failures. 
    Thursday, March 16, 2017 3:43 AM
  • Hi Glen,

    Thanks for the reply.

    I am unable to send Ok notification to exchange. From where I POST my response. WHAT is the URL.

    Below is my code. please have a look.

    string oRequest = "";
                oRequest = "<?xml version=\"1.0\" encoding=\"UTF - 8\"?>";
                oRequest += "<soap:Envelope xmlns:soap=\"\" xmlns:m=\"\" xmlns:t=\"\" xmlns:xsi=\"\">";
                oRequest += "<soap:Body><m:SendNotificationResult><m:SubscriptionStatus>Ok</m:SubscriptionStatus></m:SendNotificationResult></soap:Body>";
                oRequest += "</soap:Envelope>";

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("This is the URL which i provide to exchange when i create subricption. Is this wrong, What will be the URL");
                    req.Headers.Add("SOAPAction", "");
                    req.ContentType = "text/xml; charset=\"utf-8\"";
                    req.Accept = "text/xml";
                    req.Method = "POST";

                    //Passes the SoapRequest String to the WebService
                    using (Stream stm = req.GetRequestStream())
                        using (StreamWriter stmw = new StreamWriter(stm))
                    //Gets the response
                    WebResponse response = req.GetResponse();
                    //Writes the Response
                    Stream responseStream = response.GetResponseStream();
                    StreamReader sr = new StreamReader(responseStream);
                    string s = sr.ReadToEnd();

    Glen, If possible please help with C# code.

    I also refer below article, but not success.

    Thursday, March 16, 2017 10:02 AM
  • You need to respond to the Push call you can't just post a request back eg

    Listener gets POST from Server the response back to that POST is the Okay or Unsubscribe in SOAP. This is why people typically use a WCF or SOAP webservice as a listener as they will handle the SOAP response (there are multiple samples for doing this). In your example your website will need to respond with a SOAP response (which is just XML) to the Exchange Server Push but I've never seen anybody take that approach.

    Friday, March 17, 2017 1:41 AM
  • Thanks Glen,

    Thank you very much. Now It's working for me. Thanks for your support.

    One last query.    If we not send OK or Unsubscribe response then after three attempt our subscription is expire.

    Is there is any time out or time limit to expire the watch even I am sending Ok response each time ?

    Suppose I create today subscription and on every push request I am sending Ok response. So after one or Two month, my subscription is remain active or it's expire after some time ?

    Monday, March 20, 2017 12:16 PM
  • There is no expiry for Push notifications however your listener should be watching out for instances where your subscription in nolonger valid (eg if you don't get heartbeat within a cetain period that means the subscription would need to be recreated). Eg if somebody restarts the CAS Role Exchange server as part of normal Windows or Exchange servicing then your subscription will just stop.
    Tuesday, March 21, 2017 1:20 AM
  • Hi Glen,

    Thanks for update.

    I have one more question here :

    Initially I test my code on locally and getting PUSH response on my URL after subscribe Push notification.

    But Now i deploy my code on server and now I am not getting any push request on my URL.

    Is I need to register my Push notify URL on exchange. If Yes then where I have to register and whats the process ?

    Wednesday, March 22, 2017 5:11 AM
  • >> Is I need to register my Push notify URL on exchange. If Yes then where I have to register and whats the process ?

    No its most likely firewall related eg the Exchange server needs to be able to access the server your running this eg the Exchange server initiates the connection so that listener must publish that port through the firewall,proxies etc. You might want to look at something like to test it


    Thursday, March 23, 2017 4:16 AM
  • Thanks Glen,

    Glen I am facing a issue from 3 months ago regarding to shared calendar.

    I also post my query in various blog, but unable to get any help. below is my post :

    here is my code :

    SearchFilter sfSearchFilter = new SearchFilter.IsEqualTo(
        FolderSchema.FolderClass, "IPF.Appointment");
    FindFoldersResults ffoldres = service.FindFolders(
        new FolderId(WellKnownFolderName.Root, "emailId"),
        sfSearchFilter, fvFolderView); 

    The Result in ffolders, there is only 2-3 calendar, but there is many calendar which are shared with me by another people.

    Some in OtherCalendar folder, Some in SharedCalendar folder. These calendar are visible in my outlook client. 

    I am also go through here  :

    But this not help me.

    Friday, March 24, 2017 11:55 AM
  • Your question is unclear if you talking about external users that your subscribing to ? EWS can't read the subscription I would suggest you look at the calendars using a MAPI editor and see 1 if they exist, what the folderclass is etc. You might want to try also the EWS editor to see if that returns the calendars.
    Monday, March 27, 2017 1:02 AM