We help IT Professionals succeed at work.

Calling a .net webservice to update list item from the data retrieved by the webservice

manish_regmi
manish_regmi used Ask the Experts™
on
Hi,
I have a list with a customized newform.aspx. this forms has following fields
These all are list columns
1.SN
2.Address
3. Customer Name
4.State
5.Zip

After the SN field i have a button. Now my task is when i enter a value in the SN say 1 and click on the button then the  following fields should be
2.Address
3. Customer Name
4.State
5.Zip
automatically populated.

I have created a webservice which takes the SN parameter and returns those data in dataset.
How can i use this in that button event in the newform.aspx...
Which will provide input parameters from one list field and autopopulate the result in other list fields...
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Rajendra ShekhawatSolution Architect

Commented:

Hi,

I apologize, if I have not understand your problem completely.

Please let me know, how you have customized newform.aspx. Have you used SharePoint designer or modified control templates.

Also, you can think of writing a custom webpart which have all the controls required. It can call your webservice and populate the same. It would give you all the options to write your own custom code.

Cheers ....
Rajendra

Author

Commented:
hi rajendra,
this is a customized newform.aspx of a lsharepoint list with the list fields. this is like a form to be filled up before creating a list item. i have designed a custom form using sp designer and added a button now i dont know what to write in the click event of the button....
A solution I've used quite often in the past for this exact scenario is to use jQuery to query the webservice and then fill in all the required values.

Just attach a click event on the button using jQuery and using the $.ajax method you can query the webservice.

To make things easier I'd use a custom handler (ashx file) instead of a webservice. This custom handler would receive the SN in the querystring and would return a JSON array with the output values.

This way you can achieve your goal with a minimum of coding both on the WS side and SharePoint.

Author

Commented:
i am following the same way but it is not working do you have any sample code.

Author

Commented:
I am trying with no luck i have a input button in the NewList Form I have written an EventHandler code to communicate with the webservice but when i click on the button i get nothing
<input id="Hello" name="Button1"  type="button" value="button" />
attached is the code for the Click Event of the Button

$(document).ready(function() {

  $("# Hello ").click(function(event){

    var soapEnv =    

"<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/'> \

  <soap:Body> \

    <GetCustomerInformation xmlns='http://tempuri.org/'> \

      <CustomerName>Microsoft</ CustomerName > \

    </ GetCustomerInformation > \

  </soap:Body> \

</soap:Envelope>";

 

    $.ajax({

        url: " http://fm2k8sp-stg:20566/_vti_bin/Customers.asmx ",

        type: "POST",

        dataType: "xml",

        data: soapEnv,

        complete: processResult,

        contentType: "text/xml; charset=\"utf-8\""

    });

  });

});

Open in new window

There are a few extra spaces in your code that may be causing a problem.

On this line:

$("# Hello ").click(function(event){

It should be:

$("#Hello").click(function(event){

There is an extra space before and after Hello which will probably cause the selector not to work and your event will never fire. There are also extra spaces in the url line.

Try this:

$(document).ready(function() {

  alert('jQuery ready!');

  $("#Hello").click(function(event) {

    alert('Hello click!');

    var soapEnv =
"<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/'> \
  <soap:Body> \
    <GetCustomerInformation xmlns='http://tempuri.org/'> \
      <CustomerName>Microsoft</ CustomerName > \
    </ GetCustomerInformation > \
  </soap:Body> \
</soap:Envelope>";

    $.ajax({
        url: "http://fm2k8sp-stg:20566/_vti_bin/Customers.asmx",
        type: "POST",
        dataType: "xml",
        data: soapEnv,
        complete: processResult,
        contentType: "text/xml; charset=\"utf-8\""
    });
  });
});

There are two alerts in the code, the first should fire right away when the page loads, the second will fire when you click the button. Adding another alert in the processResult method will also help to make sure all the code is running as expected.

You should also check the status in the processResult method.

The complete delegate should have the following interface:

function processResult(xhr, status) {
  alert(status);
}

The alert above will help you debug things.

Hope this helps.



Author

Commented:
thanks for the support i did finished that code...but i m still in the same problem..
in above code where i have <CustomerName>Microsoft</ CustomerName > \
in case of microsoft here i want to pass the text of my sharepoint field whatever is in there....

You need to get that value with an expression that will be something like this:

var customer = $(':text[title = "Customer Name"]').val();

If you look at the HTML generated by the SharePoint text field control you'll see that SharePoint adds a title attribute to the input html tag with the name of the SharePoint list column.

And then concatenate that to the soap envelope you already have like so:

var soapEnv =
"<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/'> \
  <soap:Body> \
    <GetCustomerInformation xmlns='http://tempuri.org/'> \
      <CustomerName>" + customer + "</ CustomerName > \
    </ GetCustomerInformation > \
  </soap:Body> \
</soap:Envelope>";

Author

Commented:
Thanks Alot it worked Pefectly
 I am sorry to say but i still have the half of the problem
I am using the following Function to get those data into  XML
function processResult(xData, status) {

alert(status);

alert(xData.responseXML.xml);
Now i need to push back the data obtained to SharePoint Fields parsing the XML...
Any idea on this
Parsing the XML returned from your WebService is fairly easy since with jQuery you can use the same selector expressions / syntax that you use to traverse the html.

So your function should look somewhat like this:

function processData (xData, status) {
  var address = $(xData.responseXML).find('Address').text();
  $(':text[title = "Address"]').val(address);
}

The above code assumes that your webservice returns an object with a field called Address which when serialized results in an element in the XML with the same name.

The second line in the method just pushes the address value to the SharePoint field, this is quite similar to how you get the value.