Calling a web service programmatically

Calling a web service programmatically

Calling a web service programmatically

Here is an example of calling a web service client-side from a Button control within a form.

The first thing we have to do is to create the web service call in Web Services section. In our example  we will demonstrate one of the default sharepoint web services  Lists.asmx.

We give the name 'All Lists', we fix the credentials and we set the web service Url http://server/_vti_bin/Lists.asmx.

Then we fill in the method:


Code


<?xml version="1.0" encoding="utf-8"?>
  <soap:Body>
    <GetListCollection xmlns="http://schemas.microsoft.com/sharepoint/soap/" />
  </soap:Body>
</soap:Envelope>


 


and set the xPath Query: /*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='GetListCollectionResponse']/*[local-name()='GetListCollectionResult']/*[local-name()='Lists']/*[local-name()='List']  

Last we declare whice attributes want the service to show.  

 

 Our web service call 'All Lists' is ready.

 Now in the desired list we place a button control and a label control. The button will execute the call and the results will be showed in the label.

 

We  edit the button control properties and go to the 'Extra' tab.

 

 

We place the scipt: 


Code


function callback(e) {
   var c = form.GetControl("c_WSResult");
   var s = "<table cellspacing='0' cellpadding='3' width='100%'>
                     <tr><td style='background-color:#ccc'>List Name</td>
                     <td  style='background-color:#ccc'>Action</td></tr>";
   if (pf.IsEmpty(e.ErrorMessage)) {  
      for (var i=0;i<e.ListItems.length;i++) {
         name = e.ListItems[i].Title;
         url = "<a href=\"#\" onclick=\"w = window.open('http://wssdev1/Lists/" + e.ListItems[i].Title + "','','
width=800,  height=600, resizable=yes, scrollbars=yes');\" style=\"color:#cc6600\">
Open List</a>";s += "<tr><td>" + name + "</td><td>" + url + "</td></tr>";
      }
      s += "</table>";
      c.SetValue(s);
   }
   else {
      c.SetValue(e.ErrorMessage);
   }
   form.GetControl("c_WSButton").SetEnable(true);
}
form.GetControl("c_WSResult").SetValue("Loading...");
form.GetControl("c_WSButton").SetEnable(false);
form.CallWebService("All Lists", callback);


 


 As you can see this script uses the CallWebService(WebServiceName, Callback function) method.It executes the web service call and handles the result data in callback function.In this function an html table is created with 2 columns Llist Name and Action. It is populated with all the list names and a link that opens a new windoe for each list.The whole html table is set to the label  c_WSResult.

 

Before call:


During Call:


After Call: