Monday 18 November 2013

Jquery calling Exchange Web Services

I have posted the below Jquery Script which will fetch no.of unread emails by querying the exchange web services

Used the tool to generate the Soap Request Message format that is sent to the exchange web services in the below example - http://soape.codeplex.com/

<script type="text/javascript" >
function GetUnReadEmailCount() {

    var soapPacket = "<?xml version='1.0' encoding='utf-8'?>" +
                      "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'

xmlns:t='http://schemas.microsoft.com/exchange/services/2006/types'>" +
                        "<soap:Body>" +
                          "<FindItem xmlns='http://schemas.microsoft.com/exchange/services/2006/messages' xmlns:t='http://schemas.microsoft.com/exchange/services/2006/types'

Traversal='Shallow'>" +
                            "<ItemShape>" +
                              "<t:BaseShape>IdOnly</t:BaseShape>" +
                            "</ItemShape>" +
                            "<IndexedPageItemView MaxEntriesReturned='50' Offset='0' BasePoint='Beginning' />" +
                            "<Restriction>" +
                              "<t:IsEqualTo>" +
                                "<t:FieldURI FieldURI='message:IsRead' />" +
                                "<t:FieldURIOrConstant>" +
                                  "<t:Constant Value='false' />" +
                                "</t:FieldURIOrConstant>" +
                              "</t:IsEqualTo>" +
                            "</Restriction>" +
                            "<ParentFolderIds>" +
                              "<t:DistinguishedFolderId Id='inbox' />" +
                            "</ParentFolderIds>" +
                          "</FindItem>" +
                        "</soap:Body>" +
                      "</soap:Envelope>";

    jQuery.support.cors = true;
    jQuery.ajax({
        url: "http://exchange.contoso.local/ews/Exchange.asmx",
        type: "POST",
        dataType: "xml",
        data: soapPacket,
        complete: processResult,
        error: OnError,
        contentType: "text/xml; charset=\"utf-8\""
    });


}

function processResult(xData, status) {
    if (typeof xData.responseText != 'undefined') {
        alert($(xData.responseXML).find("m\\:RootFolder").attr("TotalItemsInView"));
    }
}
</script>

4 comments:

  1. how to read another mail box content? Any hint in query?

    ReplyDelete
  2. Hello.. I am getting this error 'Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.' on making call to htpps://outlook.office365.com/ews/exchange.asmx from REST jquery..
    Also using the same code as above only just changed the URL.
    Any solution Please help.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete