cancel
Showing results for 
Search instead for 
Did you mean: 

web script for cmis query

arpit_gupta
Champ in-the-making
Champ in-the-making
Hello friends,

I am using Alfresco 4.0.b community edition.
I want to search the documents using a CMIS query. There is already a default web script available for the purpose but that gives the response in atom feed format but i want the reponse in json format.

I have tried to make my own webscript for the purpose but unsuccessful.
Is it possible to make such a web script or we can not get the json response with CMIS query??

Please help me out.

Thanks,
Arpit
5 REPLIES 5

jpotts
World-Class Innovator
World-Class Innovator
The beauty of web scripts is that you can override any part of a web script that you want or add new things, like new views to an existing web script. So I tried adding a JSON response template to the extensions folder in the same folder structure as the out of the box CMIS query web script but the override didn't work. I didn't drill into the reason why, but maybe it is the same problem you were seeing.

In any case, in Alfresco 4 there is a new root object that can help you out here. The root object is called "cmis". So, I created an entirely new web script. What I'm showing you here below is quick and dirty. It could be beefed up significantly, but I'll let that be up to you.

The descriptor (org/alfresco/example/cmisquery.get.desc.xml):
<webscript>
  <shortname>Example cmis query web script</shortname>
  <description>
  <![CDATA[
  Executes a CMIS query statement against the contents of the Repository.
  ]]>
  </description>
 
  <url>/example/cmis/query?q={q}</url>
  <args>
    <arg>
        <shortname>q</shortname>
        <description>query statement to execute</description>
    </arg>
  </args>
  <authentication>user</authentication>
</webscript>

The controller (org/alfresco/example/cmisquery.get.js):
var query = args.q;
var cmisConnection = cmis.getConnection();
var cmisSession    = cmisConnection.getSession();
var results = cmisSession.query(query, false);
model.query = query;
model.results = results.iterator();

The view (org/alfresco/example/cmisquery.get.json.ftl:
{"query": "${query}",
"results": [
<#list results as res>
{"name":"${res.getPropertyValueByQueryName('cmis:name')}",
"id":"${res.getPropertyValueByQueryName('cmis:objectId')}"
}<#if res_has_next>,</#if>
</#list>
]
}

You can put these in your classpath under $TOMCAT_HOME/webapps/alfresco/WEB-INF/classes/alfresco/templates/extension or you can add them to the repository under Data Dictionary/Web Scripts Extensions.

Either way, once you deploy and refresh the web scripts index, you can enter a URL like this:
http://localhost:8080/alfresco/s/example/cmis/query?format=json&q=select%20cmis:name,cmis:objectId%2...

And you'll see a result like this:
{"query": "select cmis:name,cmis:objectId from cmis:document where cmis:name ='testwhitepaper'",
"results": [
{"name":"testwhitepaper",
"id":"workspace://SpacesStore/9a007b6a-261a-4d6d-9e34-ded4430ba1ab;1.0"
},
{"name":"testwhitepaper",
"id":"workspace://SpacesStore/3356ff7d-4172-4bd5-a826-adfa541e6ad2;1.0"
}
]
}

Look at the original OOTB CMIS query web script for ideas on paging and other things you could do to beef this up.

Hope that helps,

Jeff

applelam
Champ in-the-making
Champ in-the-making
Hi,
I'm using an alfresco 5.0.c but this script not working, please tell me why?
Thanks!

arpit_gupta
Champ in-the-making
Champ in-the-making
Thanks Jeff.

I was not aware of the cmis root object and it's methods.
This really solved my problem.

mikmontana
Champ in-the-making
Champ in-the-making
Hi all,
i'm using an alfresco 3.4 but this script not working,
I have an error like "can't find getConnection() method".
There is a solution like this webscript to have the result of a CMIS query in a xml or json for alfresco 3.4 ?

Thanks

sofia
Champ in-the-making
Champ in-the-making
Hello,
This example doesn't work in Alfresco 5.0.d , I get an ErrorReference "cmis" . Should I use an other root Object for calling cmis , instead of "cmis" ?

Thank you in advance