locked
GetListItems + lists.asmx + responseXML is null RRS feed

  • Question

  • I am trying to fetch the values of a sharepoint list using GetListItems method of web service lists.asmx.

    I cross checked the soap envelop details and everything looks fine. I am getting responseText but not responseXML.

    I tried to load responseText to XML without luck

    Here is the code:

    <script type="text/javascript">

    function GetSharePointProjectData()
    {
     
    var sa = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
        sa += "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"";
        sa += " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " ;
        sa += " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"> " ;   
        sa += " <soap:Body><GetListItems xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">";
        //sa += " <listName>{E60D3351-D664-48B1-AB54-024C6D2403C5}</listName>";
        sa += " <listName>contacts</listName>";
        sa += " <viewFields><ViewFields><FieldRef Name=\"ID\"/></ViewFields></viewFields>";
        sa += "</GetListItems></soap:Body></soap:Envelope>";
     
                         
       var ws = new ActiveXObject("Microsoft.XMLHTTP");       
       if(ws == null)
           return false;
                   
       ws.open("POST", "http://<server:port>/sites/testteamsite/_vti_bin/lists.asmx", false);
       ws.setRequestHeader("Content-Type", "text/xml;charset=utf-8");           
       ws.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetListItems");
       ws.send(sa);
      
        alert("res: " + ws.statusText + ", text : " + ws.responseText); 
        alert("res xml : " + ws.responseXML);
     
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.loadXML(ws.responseText);

        alert("xml : " + xmlDoc);

    }

     

    </script>

     

    Regards, Sujasree

    Sunday, June 26, 2011 1:57 PM

Answers

  • Unless you just need to write your own Soap envelopes I'd suggest getting SPServices (http://spservices.codeplex.com). This jQuery library does an excellent job of wrapping SharePoint's Web Services in easy to call functions. So, your getlistitems call becomes something similar to:

     

    	$().SPServices({
    			operation: "GetListItems",
    			async: false,
    			listName: "contacts",
    			CAMLViewFields: "<ViewFields><FieldRef Name='ID' /></ViewFields>",
    			 CAMLQuery: "<Query><Where><Neq><FieldRef Name='ID'/><Value Type='Number'>0</Value></Neq></Where></Query>",
    				completefunc: function (xData, Status) {
    }
    });
    


    of course, you could also look at the SPServices code itself to see how it is calling SharePoint's Web Services and compare it to what you are doing above if you just want to do it yourself.  

     

    Good luck!

    Mark


    The SharePoint Hillbilly
    Fewer Big Words... More Pretty Pictures... http://www.SharePointHillbilly.com
    Sunday, June 26, 2011 3:09 PM
  • Its working now.

    for jQuery, i have to include

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

     

    Even my original problem got solved. the problem was with the alert:    alert("res xml : " + ws.responseXML);

    alert(ws.responseXML) => returns an object

    and

    alert(ws.responseXML.xml) => returns xml

     

    Monday, June 27, 2011 8:53 PM

All replies

  • Unless you just need to write your own Soap envelopes I'd suggest getting SPServices (http://spservices.codeplex.com). This jQuery library does an excellent job of wrapping SharePoint's Web Services in easy to call functions. So, your getlistitems call becomes something similar to:

     

    	$().SPServices({
    			operation: "GetListItems",
    			async: false,
    			listName: "contacts",
    			CAMLViewFields: "<ViewFields><FieldRef Name='ID' /></ViewFields>",
    			 CAMLQuery: "<Query><Where><Neq><FieldRef Name='ID'/><Value Type='Number'>0</Value></Neq></Where></Query>",
    				completefunc: function (xData, Status) {
    }
    });
    


    of course, you could also look at the SPServices code itself to see how it is calling SharePoint's Web Services and compare it to what you are doing above if you just want to do it yourself.  

     

    Good luck!

    Mark


    The SharePoint Hillbilly
    Fewer Big Words... More Pretty Pictures... http://www.SharePointHillbilly.com
    Sunday, June 26, 2011 3:09 PM
  • Thank you Mark.

    But I am getting "jQuery undefined" error. Am i doing anything wrong?

    Regards, Sujasree

    Monday, June 27, 2011 8:13 PM
  • Its working now.

    for jQuery, i have to include

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

     

    Even my original problem got solved. the problem was with the alert:    alert("res xml : " + ws.responseXML);

    alert(ws.responseXML) => returns an object

    and

    alert(ws.responseXML.xml) => returns xml

     

    Monday, June 27, 2011 8:53 PM