I'm working on a custom HTML Form in Sharepoint 2010 and for that I want to read the structure of a list with JavaScript, which contains items and folders.
What I did was loading all the Items ignoring the folder structure and then trying to get the parent folders from the items. The problem I run into is that I don't see any method that gives me all the parent folders or just an absolute or relative path, from which I could extract the folder names. I also tried to get the file of the ListItem and get a path from there but when I try to load the file I get Cannot invoke method or retrieve property from null object. Object returned by the following call stack is null. "File"
. So my question is: Is it possible to get the path or the parent folders of a ListItem and if so how?
Here is my code:
var clientContext;
function retrieveListItems() {
clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('Skills');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View Scope="Recursive"><Query></Query></View>');
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem, 'Include(Id, DisplayName, HasUniqueRoleAssignments, FileSystemObjectType)');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args) {
var listEnum = collListItem.getEnumerator();
var listItemInfo = '';
while(listEnum.moveNext()) {
var $current = listEnum.get_current();
$current.refreshLoad();
console.dir($current);
listItemInfo += "\n" + $current.get_displayName();
var $file = $current.get_file();
$("#LOG").append("\nCurrent " + print_r($current));
$("#LOG").append("\nCurrent Path " + print_r($current.get_path()));
$("#LOG").append("\nCurrent Path ID " + print_r($current.get_path().get_identity()));
$("#LOG").append("\nFile: " + print_r($file));
clientContext.load($current.get_file(), "File");
clientContext.executeQueryAsync(function() {
$("#LOG").append("\nLevel: " + $current.get_file().get_level());
}, function(sender, args) {
$("#LOG").append("\nFailed: " + args.get_message());
});
}
$("#LOG").append(listItemInfo);
}
Also if my approach is totally wrong I would appreciate a way to iterate through the List folder structure from the root folder.
Thanks in Advance
Patrick