cancel
Showing results for 
Search instead for 
Did you mean: 

alfresco/service/sample/folder response in json

wkazmierczak
Champ in-the-making
Champ in-the-making
Hi.

Is it possible to get the response from web service, e.g.:

http://localhost/alfresco/service/sample/folder/Company Home

in json format?


Thanks
1 REPLY 1

jpotts
World-Class Innovator
World-Class Innovator
You can ask a web script for a specific response format by adding a format argument to your URL. So in this case, you could do:

http://localhost/alfresco/service/sample/folder/Company Home?format=json

Out-of-the-box, you'll see an error message because there is no JSON template defined for that web script.

Have no fear, though, because it is easy to add one.

If you go to:

http://localhost/alfresco/service/index

And find that sample web script, you can read its documentation. The page is:

http://localhost/alfresco/service/index/uri/sample/folder/%7Bpath%7D

The description says the web script lives in the Data Dictionary/Web Scripts folder so that makes it easy for us to work with it. All you need to do is add a JSON template.

To do that, go to Company Home/Data Dictionary/Web Scripts/org/alfresco/sample. Web scripts follow a specific naming convention. You'll see that the HTML template for the web script is called folder.get.html.ftl. You need to add a new one called folder.get.json.ftl. Here is a basic one to get you started:

<#escape x as jsonUtils.encodeJSONString(x)>
{
  "title": "${folder.displayPath}/${folder.name?xml}",
  "children" : [
<#list folder.children as child>
"${child.name?xml}"<#if child_has_next>,</#if>
</#list>
  ]
}
</#escape>


Once added, you can refresh the webscripts by going to http://localhost/alfresco/s/index and clicking "Refresh Web Scripts".

Now, when you go to http://localhost/alfresco/s/sample/folder/Company Home?format=json you will see something like:


{
  "title": "/Company Home",
  "children" : [
"Data Dictionary",
"Guest Home",
"User Homes",
"Shared",
"Imap Attachments",
"IMAP Home",
"Sites"
  ]
}


I'll leave it to you to modify the template to give you exactly what you want.

Jeff