1

I was wondering is there some way to get list items form SharePoint with JS synchronously. All methods I've found required to specify a callback method.

As I am building a custom JS validation for list form I need a way to lookup information on a list and get the result synchronously in the validation function.

So is there an API or some JS trick to do this. Any help will be greatly appreciated.

1
  • Just don't rely on JavaScript for all your validation. You need to repeat it on the server before you write anything back to the server. The callback method would then be used to return server-side errors to the client.
    – Mike H
    May 24, 2012 at 2:30

3 Answers 3

1

SPServices is good for retrieving list data from Sharepoint. You can set an async flag to false, although I've recently run into a situation where I could not get it to fire inline with the rest of my code. Most methods you see are basically wrappers for the xmlhttprequest object. If you have 2010 you can use it in conjuction with the listdata.svc webservice.

The call happens in a minimum of three steps:

var serviceRequest = new XMLHttpRequest();
serviceRequest.open("GET","http://site/_vti_bin/listdata.svc/listname", false);
serviceRequest.send();
var response = serviceRequest.responseText   or    serviceRequest.responseXml;

The open method can take other parameters, but the ones I've passed here are (HTTP METHOD, URI, ASYNC).

You can receive json if you want by using the setRequestHeader method between the open and send methods like this:

serviceRequest.setRequestHeader("Accept", "application/json")
2

You can force the request into running sync like @Murphy describes, but usually a better solution would be:

  • Hide the "real" submit button
  • Add your own button which performs the validation of all standard fields and sends of the request to listdata.svc if they succeed
  • In the callback either show an error message or invoke click on the "real" submit button
1

No, web services calls as well as the javascript client object model only work asynchronously. The reasoning is clear if you think about it - a synchronous call would lock up the interface until it returns - not a good web experience.

2
  • You are right in that the client object model won't allow synchronous calls, but you can make them as I described above. May 21, 2012 at 19:47
  • Derek is correct in saying that the effect on the user experience would be negative. However, XmlHttpRequest can be called synchronously or asynchronously. (yuiblog.com/blog/2006/04/04/synchronous-v-asynchronous) According to the post however, synchronous calls are a blocking action and therefore not recommended unless server and client are on the same machine or operating over a fast LAN connection. I think the point of the question is that Nikolay wants to perform the validation on the client when the error occurs and not wait for a server response for form validation.
    – Mike H
    May 24, 2012 at 3:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.