How to get document tags using REST API?

Hi,

I'd like to get (or set!) a tag to a document using the REST API.

How can this be done?

Thanks for any advice

Christian

1 votes

2 answers

2847 views

ANSWER

Is there a solution to setting and getting tags via the REST API:

/usr/bin/curl -X GET 'http://nuxeo:8081/nuxeo/api/v1/path/DAM/044/Aufspiel_242.jpg' -u Administrator:** -H 'X-NXDocumentProperties: ' does not return any information on tags.

Following the example at http://www.nuxeo.com/blog/qa-friday-remotely-searching-document-tags/ I can get all available tags via the API:

/usr/bin/curl -X GET 'http://nuxeo:8081/nuxeo/api/v1/query?query=SELECT%20%20FROM%20Tag' -u Administrator:**

and a specific tag via

/usr/bin/curl -X GET 'http://nuxeo:8081/nuxeo/api/v1/query?query=SELECT%20%20FROM%20Tag%20WHERE%20tag:label%20=%20"museumsquartier"' -u Administrator:**

In the example I see that the tags are basically nothing but relations which I should get via:

/usr/bin/curl -X POST -H "Content-Type: application/json+nxrequest" -H "Accept: application/json+nxentity,/" 'http://nuxeo:8081/nuxeo/api/v1/automation/Relations.GetRelations' -d '{ "input":"/DAM/044/Aufspiel_242.jpg", "context":{}, "params":{ "predicate": null, "outgoing":true, "incoming": true } }' -u Administrator:***

But I get an empty response though tags are set {"entity-type":"documents","entries":[]}.

When viewing the example set I wondered about the "relation:predicate" : null. Do I need another predicate in order to get tags or do I need completly other API calls.

Any help highly appreciated on this issue.

08/06/2015



Hi

After all, since no operation exists, I have developed a new operation SetDocumentTag callable from the APIREST:

@Operation(id=SetDocumentTag.ID, category=Constants.CAT_DOCUMENT, label="SetDocumentTag", description="")
public class SetDocumentTag {

    public static final String ID = "SetDocumentTag";

    private static final Log log = LogFactory.getLog(SetDocumentTag.class);

    @Context
    protected CoreSession session;

    @Param(name = "label", required = true)
    protected String label;

    @Param(name = "username", required = true)
    protected String username;

    @OperationMethod
    public void run(DocumentModel input) throws Exception {
        TagService service = Framework.getLocalService(TagService.class);

        String docId = input.getId();

        log.error("SetDocumentTag> docId=" + docId + " tag=" + label + " username=" + username);

        service.tag(session, docId, label, username);

        session.save();
    }

}

Christian

1 votes



Thanks for the operation for setting tags. My only problem is that I do not get the relations via API with the calls outlines above. Do I need a special API call to retrieve tags. Is there some special X-NXDocumentProperties set. If I use the asterisk tags are not included in the response.
08/13/2015

Hi Indeed, to get relations, you might use another API (here coded in Python)

def getRelations (self, ref):
    outgoing = u'true'

    status = self._execute (u'Relations.GetRelations',
                            input=u'doc:'+ref,
                            predicate = u'http://purl.org/dc/terms/References',
                            outgoing = outgoing)

    #print 'getRelations> ref=', ref

    if u'entries' not in status:
        return None

    return status[u'entries']

def createRelation (self, ref, object):
    es = self.getRelations (ref)
    #print 'createRelations> len(es)=', len (es)
    if es:
        for e in es:
            print 'createRelations> e[path]=', e[u'path'].encode('utf8'), ref.encode('utf8'), object.encode('utf8')
            if e[u'path'] == object:
                print 'createRelations> object already referenced'
                return es

    outgoing = u'false'

    return self._execute (u'Relations.CreateRelation',
                          input=u'doc:'+ref,
                          object=object,
                          predicate = u'http://purl.org/dc/terms/References',
                          outgoing = outgoing)

Do you need more explanations? (I use the Nuxeo 5.8 but I think >= 6.0 is still OK)

Christian

08/13/2015

Hi Christian,

thanks for the comment. I'm using GetRelations command successfully on all predicated specified in the respective vocabulary.

/usr/bin/curl -X POST -H "Content-Type: application/json+nxrequest" -H "Accept: application/json+nxentity,/" 'http://nuxeo:8081/nuxeo/api/v1/automation/Relations.GetRelations' -d '{ "input":"/DAM/044/Aufspiel_242.jpg", "context":{}, "params":{ "predicate": http://purl.org/dc/terms/References, "outgoing":true, "incoming": true } }' -u Administrator:***

The main issue at the moment is that I have no API call returning the tags set on a document. But I would need to provide them to an external application. How do you get the tags for a document via API.

I get all documents tagged with a specific tag with the following search query on ecm:tag

/usr/bin/curl -X GET 'http://nuxeo:8081/nuxeo/api/v1/query?query=SELECT%20%20FROM%20Document%20WHERE%20ecm:tag%20=%20"museumsquartier"%20AND%20ecm:mixinType%20!=%20"HiddenInNavigation"%20AND%20ecm:isProxy%20=%200%20AND%20ecm:isCheckedInVersion%20=%200%20AND%20ecm:currentLifeCycleState%20!=%20"deleted"' -u Administrator:**

But I found no other working call to get a list of all tags set on a document. Thanks for your help on this.

08/13/2015

Yes you're right!!

I guess we should add a similar new operation GetDocumentTags using the TagService:getDocumentTags function described in

http://community.nuxeo.com/api/nuxeo/5.4/javadoc/org/nuxeo/ecm/platform/tag/TagService.html

Would you try to do it?

let me know … Christian

08/13/2015


The strange thing is that there are Operations for “Tag”, “Untag” and “Remove all tags” from a document available but there seems to be no means for getting the tags set on a document via the API. I will have a look at the TagService now.

0 votes



Hi,

Yes this is strange.

One particular point on this issue:

I previously mentionned that I implemented this famous SetDocumentTag as described above. Nowadays, I gave up using it because we observed some strange behaviour in the DTB when using it. However, we never succeded to positively associate the strange DTB behaviour with setting tags. We asked Nuxeo about this suspected "bug??" but never received any answer.

We now rather wait until we migrate to >= 6.0 releases to come back to this point.

However, I encourage you to work around this Tak service….

Regards Christian

08/13/2015