Quantcast
Channel: Question and Answer » web-services
Viewing all articles
Browse latest Browse all 136

unable to use getElementsByTagNameNS with a JavaScript SOAP 1.2 call to lists.asmx

$
0
0

For reasons I do not understand, even though the call is successful and I get data back, I cannot retrieve nodes using getElementsByTagNameNS on xhr.responseXML when using SOAP 1.2 calls.

Hopefully my code below can help explain the issue:

var SITE_URL = (location.protocol + "//" + location.hostname + _spPageContextInfo.webServerRelativeUrl).replace(//$/, "");

// make an get/post request
var makeWebRequest = (function()
{
    // different ways to create xhr
    var createHTTPObject = [
        function () {return new XMLHttpRequest()},
        function () {return new ActiveXObject("Msxml2.XMLHTTP")},
        function () {return new ActiveXObject("Msxml3.XMLHTTP")},
        function () {return new ActiveXObject("Microsoft.XMLHTTP")}
    ];

    // find the way that works for this browser
    for(var i = 0; i < createHTTPObject.length; ++i)
    {
        try
        {
            createHTTPObject[i]().close
            createHTTPObject = createHTTPObject[i];
            break;
        }
        catch(e)
        {
            continue;
        }
    }

    // return a function to make the actual call
    /*
    options = {
        "url" : "...",
        "requestHeaders" : {
            "headerName" : "headerOption",
            "headerName" : "headerOption"
            ...
        },
        "postData" : "...",
        "async" : optional || true || false,
        "onComplete" : function(success, xhr)
    }
    */
    return function(options)
    {
        var async = ("async" in options && options.async !== undefined) ? options.async : true;

        var request = createHTTPObject();
        if(!request)
        {
            alert("ERROR: Unable to create an XMLHTTPRequest object.");
            return;
        }

        request.open("postData" in options ? "POST" : "GET", options.url, async);
        request.setRequestHeader("User-Agent", "XMLHTTP/1.0");

        for(headerName in options.requestHeaders)
        {
            request.setRequestHeader(headerName, options.requestHeaders[headerName]);
        }

        request.onreadystatechange = function()
        {
            if(request.readyState != 4) return;
            options.onComplete(request.status == 200, request);
            request.close;
        };

        if(request.readyState == 4) return;

        request.send(options.postData);
    };
})();

// make SharePoint web service calls
function SPWebServiceCall(service, serviceSOAPAction, soapBody)
{
    var soapRequest = '';
    soapRequest += '<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/">';
    soapRequest += '<soap:Body>' + soapBody + '</soap:Body>';
    soapRequest += '</soap:Envelope>';
    // make a web request
    makeWebRequest({
        "url" : SITE_URL + "/_vti_bin/" + service,
        "requestHeaders" : {
            "SOAPAction" : serviceSOAPAction,
            "Content-Type" : "text/xml; charset="utf-8""
        },
        "postData" : soapRequest,
        "onComplete" : function(success, xhr)
        {
            // true - 4
            console.log(success + " - "  + xhr.responseXML.getElementsByTagName("List").length);
        }
    });
}

// can't get information from the xml
function SPWebServiceCall12(service, soapBody)
{
    // make a web request
    var soapRequest = '';
    soapRequest += '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">';
    soapRequest += '<soap12:Body>' + soapBody + '</soap12:Body>';
    soapRequest += '</soap12:Envelope>';
    makeWebRequest({
        "url" : SITE_URL + "/_vti_bin/" + service,
        "requestHeaders" : {
            "Content-Type" : "application/soap+xml; charset=utf-8"
        },
        "postData" : soapRequest,
        "onComplete" : function(success, xhr)
        {
            // true - 0
            console.log(success + " - "  + xhr.responseXML.getElementsByTagName("List").length);
        }
    });
}

// will print 'true - 4'
SPWebServiceCall("lists.asmx?op=GetListCollection", "http://schemas.microsoft.com/sharepoint/soap/GetListCollection", '<GetListCollection xmlns="http://schemas.microsoft.com/sharepoint/soap/" />');
// will print 'true - 0'
SPWebServiceCall12("lists.asmx?op=GetListCollection", '<GetListCollection xmlns="http://schemas.microsoft.com/sharepoint/soap/" />');

I won’t paste it here but the responseText and does show the List data…


Viewing all articles
Browse latest Browse all 136

Trending Articles