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'm using SPService in a list for take some information from an other list.

It works just sometimes... I need to refresh lot of time before it works ....

Here is my code :

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/0.7.1a/jquery.SPServices-0.7.1a.min.js"></script>
<script type="text/javascript">


_spBodyOnLoadFunctionNames.push("getToolInfo(807)");




function getToolInfo(lastID) {

    var CamlQuery = "<Query><Where><Eq><FieldRef Name='ID'/><Value Type='Counter'>" + lastID + "</Value></Eq></Where></Query>";
    var CamlViewFields = "<ViewFields><FieldRef Name='ID' /><FieldRef Name='Title' /></ViewFields>";

  // this let me know that the function is getting called and passed the correct parameter value
  alert('function called and passed last ID of : ' + lastID);
  $().SPServices({
    operation: "GetListItems",
    async: true,
    listName: "{D28F8F6B-9EFD-4FCA-9A2E-3A419EFCF637}",
    CAMLViewFields: CamlViewFields,
    CAMLQuery: CamlQuery,
      completefunc: function (xData, Status) {
                    alert(xData.responseText);
                    console.log( xData.responseText );
                    $(xData.responseXML).SPFilterNode("z:row").each(function() { 
                            //here is where we are reading the field values and putting them in JavaScript variables
                            //notice that when we read a field value there is an "ows_" in front of the internal field name.
                            //this is a SharePoint Web Service quirk that you need to keep in mind. 
                            //so to read a field it is ALWAYS $(this).attr("ows_<internal field name>");

                            //get the title field (Speaker's Name)
                            var ID=($(this).attr("ows_ID"));
                           alert(ID);
                            //get the blog url, SharePoint stores a url in the form of <url><comma><description>
                            //We only want the <url>. To accomplish this we use the javascript "split" function
                            //which will turn <url><comma><description> into an array where the first element [0]
                            //is the url.   Catch all that? if you didn't this is another reason you should be
                            //a developer if you are writing JavaScript and jQuery :)


                        });                
                    }

        });

}

The first alert : alert('function called and passed last ID of : ' + lastID); work all the time.

The second alert : alert(xData.responseText); work 1 in 5 time

And the last alert : alert(ID); work 1 in 8 time .....

And i really don't know why or how to fix it .... And it works only on IE and chrome not in firefox ...

Any idea ?

share|improve this question
add comment

2 Answers

The problem is that you have async: true. What happens is that the code in the completefunc is running before there's any data for it to work on. In the majority of cases, you will want to set async: false if you want to do something with the data which is returned. If you simply want to set a value and assume it's going to work, then async: true makes sense.

In other words, SPServices is working fine; it's simply doing what you've asked it to do.

share|improve this answer
    
sorry to answer so late !! But I tried to put async to true and async to false and it is the same probleme !! –  pierreper Jul 19 '12 at 8:46
    
I think you're going to have to do some debugging. GetListItems should work very, very consistently. Did you switch to $(document).ready(), as suggested by PirateEric? –  Marc D Anderson Jul 19 '12 at 12:08
add comment

I've had issues in the past with alert(xData.responseText); not doing anything. Try changing it up to alert(xData.responseXML.xml);. Also maybe try setting async to false.

I'd also try utilizing the document.ready function instead of _spBodyOnLoadFunctionNames.

$(document).ready(function(){
    getToolInfo(807);
});
share|improve this answer
    
I tryed to change async to false and it's the same. –  pierreper Jul 13 '12 at 15:27
    
edited with another option –  PirateEric Jul 13 '12 at 15:32
    
finally I restart the server and it works !!!! Thanks for your help. –  pierreper Jul 19 '12 at 14:39
add comment

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.