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!
Solved! Go to Solution.
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
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
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.
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
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
tlapinsk,hi did you solve trouble related with displaying the returned content in an organized fashion?
Ask for and offer help to other Alfresco Content Services Users and members of the Alfresco team.
Related links:
By using this site, you are agreeing to allow us to collect and use cookies as outlined in Alfresco’s Cookie Statement and Terms of Use (and you have a legitimate interest in Alfresco and our products, authorizing us to contact you in such methods). If you are not ok with these terms, please do not use this website.