Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
    
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 '12 at 2:30

3 Answers 3

up vote 0 down vote accepted

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")
share|improve this answer

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.

share|improve this answer
    
You are right in that the client object model won't allow synchronous calls, but you can make them as I described above. –  Blue Murphy May 21 '12 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 '12 at 3:58

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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