Nicolas Raoul
Nicolas Raoul

Reputation: 60213

Get Alfresco NodeRef by path

I want to get the NodeRef of a document (or space) stored in Alfresco.

My code is in Java, running within Alfresco (for instance in an AMP).

How to do?

Upvotes: 1

Views: 4731

Answers (4)

FabioStein
FabioStein

Reputation: 920

Use the GET /nodes​/{nodeId}

Just provide the root folder constant Id as I did in the image Then provide the document relative path

enter image description here

Upvotes: 0

Nicolas Raoul
Nicolas Raoul

Reputation: 60213

The following Java method gets the NodeRef of the Alfresco document or space you specify:

/**
 * Get a NodeRef by its path.
 * @path as displayed by the Node Browser.
 * @return the NodeRef, or null if no NodeRef matches this path.
 */
private NodeRef getNode(String path) {
    logger.debug("Getting NodeRef for path:\"" + path + "\"");
    ResultSet results = null;
    try {
        StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
        results = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE,
            "PATH:\"" + path + "\"");
        if (results.length() == 0) {
            logger.debug("Zero matches for path: " + path);
            return null;
        }
        NodeRef nodeRef = results.getNodeRef(0);
        logger.debug("NodeRef for \"" + path + "\" is " + nodeRef);
        return nodeRef;
    }
    catch(Exception e) {
        logger.debug("Exception while searching for path: " + path, e);
        if (results != null) {
            results.close();
        }
        return null; // The node does not exist
    }
    finally {
        if (results != null) {
            results.close();
        }
    }
}

private SearchService searchService; // Be sure to set this, probably via Spring.

Be aware that each level in path must:

  • have a namespace
  • be escaped by ISO9075 (in Java code: ISO9075.encode(level))

Examples:

  • /app:company_home/app:dictionary/app:space_templates/cm:MyTemplate
  • /app:company_home/app:shared/cm:abc/cm:def/cm:My_x0020_Document.txt
  • /app:company_home/app:shared/cm:_x0031_23

To find out what is the path of a particular document or folder, the Node Browser (in Admin Tools) is your friend:

Primary path in Alfresco Node Browser, ISO9075

I make the method above public domain, please fix or comment if you notice anything that can be improved, thanks! :-)

Upvotes: 3

Lista
Lista

Reputation: 2246

Path queries and not the fastest, though, especially if you're on Lucene. You should think of an alternative ways to find what you're looking for.

Upvotes: 0

Gagravarr
Gagravarr

Reputation: 48336

The easiest way is probably using the NodeLocatorService and the XPath locatorName + an xpath expression

Under the hood, that uses the search service, but it wraps up a lot of the complexity for you!

To use it, get the NodeLocatorService injected into your bean, then do something like:

 Map<String,Serializable> params = new HashMap<>();
 params.put("query", "/x:path/to:node/pa:th");
 NodeRef nodeRef = nodeLocatorService.getNode("xpath",null,params);

Other NodeLocators exist for other lookups, and it's also available remotely via /alfresco/service/api/nodelocator/{node_locator_name}?params

Upvotes: 3

Related Questions