cancel
Showing results for 
Search instead for 
Did you mean: 

List all folder, subfolders, & files via webscript

tlapinsk
Champ on-the-rise
Champ on-the-rise

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!

1 ACCEPTED ANSWER

‌ thanks for following up. I was able to get all of the content returned and organized. I ended up using two web scripts to achieve the results we wanted. One to hold content and another to make AJAX calls to grab the content.

View answer in original post

8 REPLIES 8

kalpesh_c2
Star Collaborator
Star Collaborator

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

ContCentric

calvo
Star Collaborator
Star Collaborator

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

tlapinsk
Champ on-the-rise
Champ on-the-rise

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.

andy1
Star Collaborator
Star Collaborator

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

tlapinsk
Champ on-the-rise
Champ on-the-rise

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

  • Subfolder
    • doc 1
    • doc 2
    • doc 3
  • doc 1
  • doc 2
  • doc 3

Folder 2

  • Subfolder 1
    • doc 1
    • doc 2
  • Subfolder2
    • doc 1
    • doc 2
  • doc 1
  • doc 2
  • doc 3

Folder 3

  • Subfolder 1
    • doc 1
  • doc 1
  • doc 2

‌ do you have any tips for organizing it in this fashion?

emerald911
Champ in-the-making
Champ in-the-making

tlapinsk‌,hi did you solve trouble related with displaying the returned content in an organized fashion?

‌ thanks for following up. I was able to get all of the content returned and organized. I ended up using two web scripts to achieve the results we wanted. One to hold content and another to make AJAX calls to grab the content.

emerald911
Champ in-the-making
Champ in-the-making

‌, can you give any clue how did you form the structure? Was it one lucene query and then formatting of content or it was multiple queries one by one to build it? Best Regards