Hi,
below is my code and what it does is it gets a list of folders(recursively) from a given folder. However, I am having trouble with indentation logic that I need to write so that I can build a folder hierarchy. Please help. right now the code below displays all folders but with no indentation.
<script type="text/javascript" language="javascript">
ExecuteOrDelayUntilScriptLoaded(getFolders, "sp.js");
function getFolders()
{
context = new SP.ClientContext.get_current();
var web = context.get_web();
//get the list
var list = web.get_lists().getByTitle('Project Documents');
rootFolder = list.get_rootFolder();
var query = new SP.CamlQuery.createAllFoldersQuery();
//set the folder where the Query should start looking for folders recursively. The Url will be passed as parameter from the caller that calls getFolders() function.
query.set_folderServerRelativeUrl('/sites/cof/Project%20Documents/3%2E%20Submittals%2C%20RFIs%20and%20FWDs');
allItems = list.getItems(query);
//send XML requests to client.svc and wait for the returned JSON
context.load(allItems);
context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
}
function success()
{
var folderNames = "";
var itemEnumerator = this.allItems.getEnumerator();
while(itemEnumerator.moveNext())
{
var currentItem = itemEnumerator.get_current();
if(currentItem.get_fileSystemObjectType() == '1')
{
//write logic to check if current path contains the previous path and then indent.
folderNames += "<div><img src='sites/cof/_layouts/Images/folder.gif' alt='folder'/>" + currentItem.get_item('FileLeafRef') + "</div>";
}
}
$("#folderTree").html(folderNames);
}
function failed(sender, args)
{
alert("failed. Message:" + args.get_message());
}
</script>