How can i move file/folder of Nuxeo to Trash with REST API

841 views Asked by At

From Nuxeo REST API Document, I can see the deleted file/folder in TRASH with this code

SELECT * FROM Document WHERE ecm:mixinType != 'HiddenInNavigation' 
AND ecm:currentLifeCycleState = 'deleted' AND ecm:isProxy = 0 AND ecm:isCheckedInVersion = 0

But how can I update the Document with ecm:currentLifeCycleState to move Document to TRASH?

Thank you

2

There are 2 answers

9
Jake W On BEST ANSWER

Here is the code I used to move a document to trash.

  public boolean deleteDocument(Session session, String documentId) throws Exception {
    try {
      Document document = getDocumentById(session, documentId);
      // When delete a document, only move it to Trash
      session.newRequest("Document.SetLifeCycle")
          .setInput(document).set("value", "delete").execute();
      return true;
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      throw e;
    }
  }

And the following code would delete the document permanently.

  public static boolean deleteDocument(Session session, String documentId) throws Exception {
    try {
      Document document = getDocumentById(session, documentId);

      // Delete the document permanently
      session.newRequest("Document.Delete").setInput(document).execute();
      return true;
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      throw e;
    }
  }

One way is to find the documents in trash first via a NXQL, like:

SELECT * FROM Document WHERE ecm:currentLifeCycleState = 'deleted'

And then delete them permanently via the method above. There is also a post mentioning this: http://answers.nuxeo.com/questions/1830/actioncommand-to-permanently-delete-all-document-in-trash

Utility method: fetch a document by documentId:

  public static Document getDocumentById(Session session, String documentId) throws Exception {
    try {
      return (Document) session.newRequest("Document.Fetch").set("value", documentId)
          .setHeader(Constants.HEADER_NX_SCHEMAS, "*").execute();
    } catch (Exception e) {
      log.error("Failed to fetch document: " + e.getMessage(), e);
      throw e;
    }
  }
0
Florent Guillaume On

You should use the Document.SetLifeCycle operation to follow the delete transition.