Reputation: 60213
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
Reputation: 920
Just provide the root folder constant Id as I did in the image Then provide the document relative path
Upvotes: 0
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:
ISO9075.encode(level)
)Examples:
To find out what is the path of a particular document or folder, the Node Browser (in Admin Tools) is your friend:
I make the method above public domain, please fix or comment if you notice anything that can be improved, thanks! :-)
Upvotes: 3
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
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