How to Access SharePoint Web Services via Javascript?

Been tasked by my boss with another tight deadline project (understand client’s requirement and developed 2 mobile app within 1 month). Both of the apps required to read data from SharePoint Web Services using Javascript.

Before we proceed, it is important to know what are the types of SharePoint web services that are available to us? Click here for a list of SharePoint services.

Next, we need to understand how to read and call web services’ methods. In the example below, we will demonstrate how we derived and constructed our SOAP message to retrieve list’s items (API: List.GetListItems method):

  1. Always wrapped our SOAP message block with the following header and footer:
    Header

    <soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>
     <soapenv:Header/>
     <soapenv:Body xmlns='http://schemas.microsoft.com/sharepoint/soap/'>
    

    Footer

     </soapenv:Body>
    </soapenv:Envelope>
    
  2. Construct the web service’s method and parameters exactly as shown in MSDN. In our example, we will input listName and sort the result according to the title:
    <GetListItems>
     <listName>{A4E4AA50-CB01-48C4-9D24-DD6404CD70C1}</listName>
     <query>
      <Query>
       <OrderBy>
        <FieldRef Ascending='TRUE' Name='Title'/>
       </OrderBy>
      </Query>
     </query>
    </GetListItems>
    

Lastly wrapped the SOAP message that you form above and sent it thru using jQuery.ajax method (example like the one below):

$.ajax({
 type: "POST",
 url: url /* the url will be as stated in MSDN Web Reference, for all list related services, it will be http://<Site>/_vti_bin/Lists.asmx*/,
 data: q.join("") /* the soap message that we have construct earlier*/,
 contentType: "text/xml; charset=utf-8",
 dataType: "html" /* can be xml */,
 success: function(xdata){
  result.css("color","green").html($('<div/>').text(xdata).html());
 },
 error: function(a,b,c){
  result.css("color","red").html("Unable to connect ("+c+").");
 }
});

Leave a Reply

Your email address will not be published. Required fields are marked *