The following code successfully loops over all my files in a particular Document Library and outputs the name of each file. However, there are additional properties (custom properties/columns that are not part of the Document content type) that I need to enumerate. get_item()
is not a valid method on the SP.File
type, so I can't just pass in the property name that I'm looking for. I suspect I'll need to pass an Include
argument to the .load()
method, but I've been unsuccessful with that as it errors out telling me that the property names I've included don't exist.
<script src="https://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
<script>
// Load the required SharePoint libraries.
$(function () {
var ctx = null;
var collListItem = null;
// Function to retrieve a query string value.
// For production purposes you may want to use
// a library to handle the query string.
function getQueryStringParameter(paramToRetrieve) {
var params =
document.URL.split("?")[1].split("&");
var strParams = "";
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params[i].split("=");
if (singleParam[0] == paramToRetrieve)
return singleParam[1];
}
}
// Get the URI decoded URLs.
hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
// The js files are in a URL in the form: web_url/_layouts/15/resource_file
var scriptbase = hostweburl + "/_layouts/15/";
// Load the js files and continue to the execOperation function.
$.getScript(scriptbase + "SP.Runtime.js",
function () {
$.getScript(scriptbase + "SP.js", mainFunction);
}
);
var siteUrl = "https://company.sharepoint.com/sites/theSite";
var docSetUrl = "https://company.sharepoint.com/sites/theSite/docLibraryName/docSetName";
var ctx = null;
var files = null;
function mainFunction() {
getFiles(docSetUrl);
}
function getFiles(folderUrl) {
ctx = new SP.ClientContext(siteUrl);
files = ctx.get_web().getFolderByServerRelativeUrl(folderUrl).get_files();
ctx.load(files);
ctx.executeQueryAsync(success, error);
}
function success() {
console.log("success");
for (var i = 0; i < files.get_count(); i++) {
var file = files.get_item(i);
console.log(file.get_name());
}
}
function error(sender, args) {
console.log("error");
console.log(args.get_message());
}
});
</script>
You need to add in the ListItemAllFields property.
Copied from this answer: