Alfresco REST Core API find node by path

907 views Asked by At

I am developing an Alfresco client based on REST APIs.

But I haven't found a method that allowsto retrieve a node with its path.

[ base url: /alfresco/api/-default-/public/alfresco/versions/1 , api version: 1 ] 

Could you tell me how i can do it ?

2

There are 2 answers

0
Arjun On BEST ANSWER

The below code will return the noderef as you are expecting from the folder/file path.

public static void main(String[] args) throws IOException {
        client = new AlfrescoClient.Builder().connect("http://localhost:8080/alfresco", "admin", "admin").build();
        NodeRepresentation node = findNodeByPath("Sites/testsite/documentLibrary/TestFolder/UploadTest.txt");
        System.out.println(node == null ? "null" : node.getName());
    }

private static NodeRepresentation findNodeByPath(String path) throws IOException {
    if (path.startsWith("/"))
        path = path.substring(1);
    if (path.endsWith("/"))
        path = path.substring(0, path.length() - 1);
    return findNodeByPath("-root-", path);
}

private static NodeRepresentation findNodeByPath(String parentNodeId, String path) throws IOException {
    String[] pathParts = path.split("/");
    String name = pathParts[0];
    String remaining = pathParts.length == 1 ? "" : path.substring(name.length() + 1, path.length());
    List<NodeRepresentation> children = client.getNodesAPI().listNodeChildrenCall(parentNodeId).execute().body()
            .getList();
    for (NodeRepresentation child : children) {
        if (child.getName().equals(name)) {
            return pathParts.length == 1 ? child : findNodeByPath(child.getId(), remaining);
        }
    }
    return null;
}
2
Lista On

You can try using AFTS and PATH operator, it should work. You are basically writing a query, not using a specific API method for "find by path". For example:

{
  "query": {
    "language": "afts",
    "query": "PATH:'/app:company_home/st:sites/cm:swsdp/cm:documentLibrary/cm:Agency_x0020_Files//*' OR PATH:'/app:company_home/st:sites/cm:swsdp/cm:documentLibrary/cm:Budget_x0020_Files//*'"
  }
}