10-24-2017 01:01 PM
Hey Alfresco Community,
I've done a fair bit of research, but haven't been able to find the right answer. I am trying to create a webscript that will grab all folders, subfolders, and files in a JavaScript webscript (JSON response).
Currently, my model looks like this:
// search for folder within Alfresco content repository
var folder = roothome.childByNamePath("PATH");
// validate that folder has been found
if (folder == undefined || !folder.isContainer) {
status.code = 404;
status.message = "Folder " + " not found.";
status.redirect = true;
}
// construct model for response template to render
model.folder = folder;
I then am able to get a JSON response of the children using this Freemarker:
<#assign datetimeformat="EEE, dd MMM yyyy HH:mm:ss zzz">
{"corporates" : [
<#list folder.children as child>
{
"folder" : "${child.properties.name}"
}
</#list>
]
}
Which returns the following:
{"corporates" : [
{
"folder" : "Example Folder 1"
}
{
"folder" : "Example Folder 2"
}
{
"folder" : "Example Folder 3"
}
{
"folder" : "Example Folder 4"
}
]
}
This first response is great, but there are further subfolders/files beneath the four folders returned above. The end result will need to include these subfolders/files.
I have also tried the following threads advice, but it doesn't seem to list the files/folders nicely. See link here for the thread.
A second stackoverflow post gives advice, but I wasn't able to replicate billerby's advice correctly to traverse and store subfolders/files in a model. I believe this could be the right direction, but couldn't get it to run correctly.
Any help pointing me in the right direction would be much appreciated!
04-03-2018 06:24 PM
10-25-2017 03:08 AM
Hi tlapinsk
You can use lucene search to get sub files and folders by path. Below is the sample code that you can use by replacing path as per your folder.
var folder = search.luceneSearch("+PATH:\"/app:company_home/cm:Test_x0020_Folder//*\" AND
(TYPE:\"cm:content\" OR TYPE:\"cm:folder\")");
Thanks,
Kalpesh
10-25-2017 03:28 AM
Hi,
In this link you can find a concept test to list folders contained in a given folder.
Maybe you can modify this code to adapt it to your requirements.
netic360: :: Alfresco. Crear webscript para listar carpetas
Regards,
clv
10-25-2017 12:10 PM
Hi Kalpesh Patel and calvo _
Thank you so much for the responses. I ended up creating a macro in Freemarker (per the advice of this stackoverflow response).
Here the the code that I am using the elicit a JSON response:
{"corporates" : [
<@recurse_macro node=folder depth=0/>
]
}
<#macro recurse_macro node depth>
<#list node.children?sort_by(["properties","name"]) as child>
{
"Name" : "${child.properties.name}",
"URL" : "${child.url}",
"serviceURL" : "${child.serviceUrl}",
"shareURL" : "${child.shareUrl}",
"ID" : "${child.id}",
"Type" : "${child.typeShort}"
},
<#if child.isContainer>
{
<@recurse_macro node=child depth=depth+1/>
}
</#if>
</#list>
</#macro>
I will be modifying it further to return more readable JSON and then using AJAX to call/parse the JSON from a second webscript.
If you have any tips on using AJAX to make calls between web scripts, please let me know!
Cheers.
10-26-2017 03:55 AM
Hi
Using a query, as suggested, is a much better solution than walking the tree.
Someone is going to put lots of children in a folder and structure in the hierarchy and then what?
Regards
Andy
10-26-2017 12:43 PM
That is a great point. Even if it recursively walks the tree, it'll become slow and bloated over time.
I've implemented the lucene query as suggested, but having trouble displaying the returned content in an organized fashion. Ideally I would like it to look generally like this:
Folder 1
Folder 2
Folder 3
04-03-2018 10:39 AM
tlapinsk,hi did you solve trouble related with displaying the returned content in an organized fashion?
04-03-2018 06:24 PM
04-04-2018 02:52 AM
Explore our Alfresco products with the links below. Use labels to filter content by product module.