GetListItems Syntax with Variable Subsite/Subfolder Location

Apr 25, 2014 at 5:47 PM
Edited Apr 25, 2014 at 5:55 PM
I have a custom JavaScript-based page that allows users to create a list of links to files in other locations. I'm now looking for a way to provide the Modified date as additional information appended to the end of the link text. That is, if a user provides the text My File and links it to a file at location sharepointsite.com/sites/unit/subUnit/Shared Documents/Folder 1/Subfolder 1/My File.docx, after the script runs the text should read My File (4/25/2014). The date format isn't so relevant as the fact that it appears.

I've gotten the script to the point where I can determine the path and file name, and pass these variables into GetListItems, but I seem to be having problems with the syntax of GetListItems. Can you spot anything wrong here?
$().SPServices({
    operation: "GetListItems",
    async: false,
    webURL: actualFilePath,
    listName: actualListName,
    CAMLViewFields: "<ViewFields><FieldRef Name='FileRef' /><FieldRef Name='Title' /><FieldRef Name='Modified' /></ViewFields>",
    CAMLQuery: "<Query><Where><Eq><FieldRef Name='FileRef' /><Value Type='Url'>" + actualFileName + "</Value></Eq></Where></Query>",
    completefunc: function (xData, Status) {
        itemTitle = $(this).attr("ows_Title");
        itemMod = $(this).attr("ows_Modified");
        // Apply the modified date to the text
    }   
});
Currently, the itemTitle and itemMod assignments are returning undefined. The variables I'm passing into GetListItems contain information somewhat like this:

actualFilePath: "/sites/unit/subUnit/"
actualListName: "Shared Documents"
actualFileName: "Shared Documents/Folder/Subfolder/FileName.docx"

The purpose for using variables is because file paths are going to vary from file to file. I'm not sure if the problem exists in the data I'm passing, the GetListItems syntax, or a combination of the two.

Thanks for any help you can provide!
Apr 25, 2014 at 7:53 PM
Your code to process the GetListItems result set is not correct. Take a look at the documentation page for that method which will have examples on how to process the results.



--
Paul T.

-- Sent from Mobile

Apr 28, 2014 at 1:37 PM
Edited Apr 28, 2014 at 2:24 PM
Paul, thanks for your response, and for your help on previous questions. I don't know what I was thinking putting those assignment statements outside the SPFilterNode, so thanks for setting me straight there. Having moved them inside, I'm still not getting a response, and I'm not sure where the problem is. Here's what I have now:
// The following are dynamically determined in the actual code
// These explicit values are test data
actualFilePath = "/sites/unit/subUnit";
actualListName = "Shared Documents";
actualFileName = "Shared Documents/folder/subFolder/FileName.docx";

$().SPServices({
    operation: "GetListItems",
    async: false,
    webURL: actualFilePath,
    listName: actualListName,
    CAMLQuery: "<Query><Where><Eq><FieldRef Name='FileRef' /><Value Type='Lookup'>" + actualFileName + "</Value></Eq></Where></Query>",
    CAMLViewFields: "<ViewFields><FieldRef Name='EncodedAbsUrl' /><FieldRef Name='ContentType' /><FieldRef Name='Modified' /></ViewFields>",
    CAMLQueryOptions: "<QueryOptions><IncludeMandatoryColumns>False</IncludeMandatoryColumns><ViewAttributes Scope='Recursive' /></QueryOptions>",
    completefunc: function (xData, Status) {
        $(xData.responseXML).SPFilterNode("z:row").each(function() {
            itemName = $(this).attr("ows_FileRef");
            itemMod = $(this).attr("ows_Modified");
            modDate = " (" + itemMod + ")";
        });
    }   
});
The problem area appears to be in the CAMLQuery statement; if I remove that, GetListItems starts returning things. What is a good statement to use to narrow results down to only the one item I have specified in actualFileName?
Apr 28, 2014 at 3:07 PM
Edited Apr 28, 2014 at 3:08 PM
The 'FileRef' attribute as a lookup includes the record ID and everything in the filepath/name after the domain, delimited by ";#"
If I had a file with an ID of 25, located at
"https://www.mydomain.com/sites/sitename/Library Name/File Name.docx",
the "FileRef" field for that file is...
25;#sites/sitename/Library Name/File Name.docx
Using my example, your query will work if your argument "actualFileName" is "sites/sitename/Library Name/File Name.docx." I suspect you may not be forming your variable value correctly.

Geoff
Apr 28, 2014 at 3:24 PM
Thank you both so much-- that did it. I'm now returning the modified date just as I was trying to do. I appreciate your input. As Geoff noted, the trouble lay in my not including the sites/sitename/ prefix. Here's the assignment that causes the query to work:
actualFileName = "sites/unit/subUnit/Shared Documents/folder/subFolder/FileName.docx";
The query itself didn't change, so the problem was completely due to the formation of the path in actualFileName.

Thanks again, gentlemen!