Showing posts with label Exchange Web Services. Show all posts
Showing posts with label Exchange Web Services. Show all posts

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>