cancel
Showing results for 
Search instead for 
Did you mean: 

Getting path to a folder through freemarker template in alfresco

juilee
Champ in-the-making
Champ in-the-making
Hi, would like to get the path to all the sub-folders of a parent folder, from a freemarker template.

I tried the following, but it throws error not able to find the node: "${url.serviceContext}/api/node/content/workspace/SpacesStore/{child.nodeRef.id}/{child.name}"

"${url.serviceContext}/api/node/content/${child.nodeRef.storeRef.protocol}/${child.nodeRef.storeRef.identifier}/${child.nodeRef.id}/{child.name?url}"


Am I missing something ?


Thanks
7 REPLIES 7

sanket
Champ on-the-rise
Champ on-the-rise
The Response Templates section in the link below may help you.
https://wiki.alfresco.com/wiki/Web_Scripts_Examples

sanket
Champ on-the-rise
Champ on-the-rise
JS file :

function main()
{
   var results = [];
  
   if(args["nodeRef"] != null) {
   
   var objNodeRef = search.findNode(args["nodeRef"]);
   var nodeChilds = [];
   
   if(objNodeRef != null) {
      nodeChilds = objNodeRef.children;
      
      for each (var result in nodeChilds)
      {
         
             var resultObj =
             {
                 item: result
             };
             resultObj.selectable = "true";
             results.push(resultObj);
         
      }
   }
   }
   model.results = results;   
}

main();


JSON Response file code :


{
   "data":
         {
           "items":
                  [
                     <#list results as row>
                        {
                           "name": "${row.item.properties.name!""}",
               
                           "type": "${row.item.typeShort}",
                           "isContainer": ${row.item.isContainer?string},
                        
                           <#if row.item.properties.title?exists>
                              "title": "<#escape x as jsonUtils.encodeJSONString(x)>${row.item.properties.title!""}</#escape>",
                           </#if>
                           
                           <#if row.item.properties.description?exists>
                              "description": "<#escape x as jsonUtils.encodeJSONString(x)>${row.item.properties.description!""}</#escape>",
                           </#if>
                           
                           <#if row.item.properties.modified??>"modified": "${xmldate(row.item.properties.modified)}",</#if>
                           <#if row.item.properties.modifier??>"modifier": "${row.item.properties.modifier}",</#if>
                           <#if row.item.siteShortName??>"site": "${row.item.siteShortName}",</#if>
                           "displayPath": "${row.item.displayPath!""}",
                           "nodeRef": "${row.item.nodeRef}"<#if row.selectable?exists>,
                           "selectable" : ${row.selectable?string}</#if>
                        }<#if row_has_next>,</#if>
                     </#list>
                  ]
         }
}

sanket
Champ on-the-rise
Champ on-the-rise
You just need to add one more desc file along with above two files.

<webscript>
  <shortname>Get children</shortname>
  <description>Get children details</description>
  <url>/api/folder/getchildren</url>
  <format default="json"/>
  <authentication>user</authentication>
  <transaction allow="readonly">required</transaction>
</webscript>

juilee
Champ in-the-making
Champ in-the-making
Hi Sanket,

Thanks a lot for the detailed answer.
I am relatively new to alfresco so this might sound little silly but as far as I understand, we can display contents using freemarker template in alfresco. To process the JSON response received, I should iterate over the results variable in a freemarker template to display the contents. Is that correct?

Also earlier I was passing the path of the folder in the url as:
http://localhost:8080/alfresco/service/sample/folder/path.
And it would be taken by the js as
var folder = roothome.childByNamePath(url.extension);
In this case, how do we pass the args?

And out of the variables we get in the JSON response, which would give the exact path to the folder (not the display path but actually will take the user to the folder by clicking on it). Earlier I had tried node.url, node.downloadUrl, node.nodeRef, etc but none takes me to the folder.

I would appreciate your help on this.

Regards,
Juilee

_jan
Champ on-the-rise
Champ on-the-rise
Hi
You are right that freemarker is used for display contents which you can produce by Javascript file. By freemarker you can make json, xml, html or what type of response you need. In freemarker you can iterate with
<#list>
.. Here is manual for it http://freemarker.org/docs/ref_directive_list.html .

You can pass arguments there by adding to url
http://localhost:8080/alfresco/service/sample/folder/path?yourArg=yourArg
and in Javascript you can get it by
args.yourArg
.
It is good to specify what arguments there should be in webscript in
.desc.xml
file like this
<webscript>
  <shortname>Get children</shortname>
  <description>Get children details</description>
  <url>sample/folder/path?yourArg={yourArg}</url>
  <format default="json"/>
</webscript>


About that url. I had some days ago same problem and I havent found how to get that link so I made it by myself. I dont know if it is good aproach but I havent found anything better.
yourNodeRef.displayPath.replace("Company Home/", "")

this will give you filter so you can pass it like link:
'<a href="repository#filter=path|' + pathFromWebscript + '">'

sanket
Champ on-the-rise
Champ on-the-rise
If you are using alfresco share, then the exact url pointing to the folder should be something like : (provided I have created this folder hierarchy in my repository - CD > ABC
http://localhost:8080/share/page/repository#filter=path%7C%2FCD%2FABC%7C&page=1
Lets say if your folder hierarchy is something like : One > Two  , then your URL would be like :
http://localhost:8080/share/page/repository#filter=path%7C%2FOne%2FTwo%7C&page=1

So you need to append the alfresco url as prefix to your path.

But, if you are using alfresco explorer (and not alfresco share), then your url directly to the folder would require folder nodeRef.
Something like :
http://localhost:8080/alfresco/n/browse/workspace/SpacesStore/f7af6b49-3296-4474-853b-db357061c53b

So again, append the alfresco url as prefix to the nodeRef
NOTE: Your nodeRef contains ':/'. You need to remove ':/' from the nodeRef and then put it in the url.
Actual nodeRef  : workspace://SpacesStore/f7af6b49-3296-4474-853b-db357061c53b
NodeRef appended in url : workspace/SpacesStore/f7af6b49-3296-4474-853b-db357061c53b

juilee
Champ in-the-making
Champ in-the-making
Hi Sanket,

Thanks ! I am using Alfresco share and I did the same thing as you mentioned for the share part. I was just wondering if that is the right way to do it OR is there a better way?

Regards,
Juilee