Chaining SPServices calls

Jun 19, 2012 at 12:52 AM

I am sure there should be a better way to do this but I have not found one yet.  If there is a better way let me know.

 

I need to list a user's URL & Titles that have the "I Like It" tag so I am calling GetTagUrlsByKeyword followed by the GetWeb using the URL returned from GetTagUrlsByKeyword.  There is a lot of bouncing around to get this which I don't like but trying to make it work anyway.

It seems I am not properly calling GetWeb  from within the completefunc of GetTagUrlsByKeyword because the Title never gets updated from the 'foo' placeholder.

Thanks for the help and reading my post!


 $(".GetUrLikes").click(function () {
            alert("Handler for .GetUrLikes called.");

            $(this).SPServices({
				operation: "GetTagUrlsByKeyword",
				keyword: "I Like It",
				async: true,
				completefunc: function (xData, Status) {
					$("#MyLikeList").html("").append("<h3>Sites 'Liked' by you..</h3>");
					
						$(xData.responseXML).find("soap\\:Envelope").find("soap\\:Body").find("GetTagUrlsByKeywordResponse").find("GetTagUrlsByKeywordResult").find("string").each(function() {
							//$("#MyLikeList").append(this.Text());
							var siteURL = this.text;
							var siteTitle = 'foo';

							$().SPServices({
								operation: "GetWeb",
								webURL: siteURL ,
								async: false,
								completefunc: function (xData, Status) {
									$(xData.responseXML).find("soap\\:Envelope").find("soap\\:Body").find("GetWebResponse").find("sWebMetadata").find("Title").each(function(){
										siteTitle = this.text;	
										alert("d");
									});
										
								}
							});
							
							$("#MyLikeList").append("<a href='" + siteURL  + "' target='_blank'>" + siteTitle +"</a>" + "</br>");
						});
                }

            });

        });


    });

Jun 19, 2012 at 12:39 PM

My first suggestion is to set the first async value to false as well. If you are doing a loop, sometimes this will just not work unless you set async to false. This should force the second function to finish before the first loop reiterates. Hope that makes sense!

Jun 19, 2012 at 10:25 PM

Thank you.  Either way async true or false the GetWeb call is not executing in my example.

 

I am thinking that the call to GetWeb does not have a proper selector for my code because it is nested in the previous call's completefunc.

 

What should be selector be to call a SPService from within a completefunc ?

Coordinator
Jun 19, 2012 at 11:04 PM

There are a few things you've got in your script that aren't right. You're mixing some JavaScript and jQuery sytax incorrectly, etc.

You don't need to call SPServices on a selected object (this alone may be breaking things).

$().SPServices({

You need

  async: false,

so you can be sure that the XML is avaialbe before your completefunc runs.

Next, your selectors are more complex than need be. Try this:

$(xData.responseXML).find("GetTagUrlsByKeywordResult").find("string").each(function()

Next, to get the siteUrl:

var siteURL = $(this).text();

then to be sure you have a valid value:

alert(siteURL);

Hopefully this will get you moving forward; itworks in my environment. It's always best to start simple and build from there.

 $().SPServices({
  operation: "GetTagUrlsByKeyword",
  keyword: "I Like It",
  async: false,
  completefunc: function (xData, Status) {
alert(xData.responseText);
   $(xData.responseXML).find("GetTagUrlsByKeywordResult string").each(function() {
    var siteURL = $(this).text();
alert(siteURL);
   });
  }
 });

M.