How do you get an SPServices parameter from within the completefunc function?

Jul 26, 2012 at 12:15 AM
Edited Jul 26, 2012 at 1:35 AM

I am reading data from a specific view within a document library and then constucting an html <ul> list within a Page stored in a separate site.

It is fairly straightforward and I have it working as I would like, however to make it better/more robust I would like to be able to "get" some of the SPServices "parameters" from within the completefunc code itself.

Here is a snippet of my code below:

$().SPServices({
        operation: "GetListItems",
        webURL: "http://server/a/b", 
        listName: "Documents",
        viewName: "{7F092717-3E9A-413B-A22D-7F2C25DDBA89}",
        completefunc: function (xData, Status) {
                var cheatURL = "http://server/a/b/Documents";
                var output = "";

                $(xData.responseXML).SPFilterNode("z:row").each(function(i) {
                                // output here
                                output += "<li><a href='" + cheatURL + otherDetailsPulledFromResponseXML + "'>link name</a></li>"
                }
        }
})

As you can see, I had to "cheat" and manually reconstruct the variable cheatURL as I wasn't able to work out a way of getting the parameter values for webURL and listName.

Initially I tried the following:

var prefixURL = this.webURL + "/" + this.listName;

Unfortunately this.parameterName doesn't work. I tried many other methods as well, but as I am a newbie I couldn't work it out.

Apologies if this is basic JS, but I have had a good crack searching with google and in codeplex, but couldn't find anything to help.

Thanks in advance

Jul 26, 2012 at 1:34 AM
Edited Jul 26, 2012 at 1:34 AM

I've worked out a better approach - but still would like to know if it is possible to "get" the SPServices parameters.

My new approach is as follows:

var g_webURL = "http://server/a/b";
var g_listName = "Documents";

{
        operation: "GetListItems", 
        webURL: g_webURL, 
        listName: g_listName, 
        viewName: "{7F092717-3E9A-413B-A22D-7F2C25DDBA89}",
        completefunc: function (xData, Status) {
                var pathURL = g_webURL + "/" + g_listName;
                var output = "";

                $(xData.responseXML).SPFilterNode("z:row").each(function(i) {
                                // output here
                                output += "<li><a href='" + pathURL + otherDetailsPulledFromResponseXML + "'>link name</a></li>"
                }
        }
})

This does the trick and is marginally better than my first approach.

Jul 26, 2012 at 1:17 PM

I typically do not use the viewName property, but you should get back the FileRef attribute regardless.  Try updating your code with this snippet:

        completefunc: function (xData, Status) {
&#65279;                var pathURL = g_webURL + "/" + g_listName;
                var output = "";
console.log( "status: " + Status );
console.log( "xml: " + xData.responseText );
                $(xData.responseXML).SPFilterNode("z:row").each(function(i) {
                                // output here
                                output += "
link name
"
                }
        }

This will output the data to the console.  You'll easily be able to tell what kind of attributes are returned and then adjust accordingly.

Cheers,
Matt