Thursday, 27 December 2018

Get node from path in Alfresco

At times, you might face a scenario where instead of getting the path from noderef, you want noderef from path.
In this case, nodeService API has no direct way.
You can do it in a couple of ways like:
1) Use nodeLocatorService
String finalPath = "./app:company_home/st:sites/cm:test/cm:documentLibrary/cm:abc/*";
// above xpath query is used to get all immediate children inside abc folder; it is equivalent to the //following fts-alfresco query:
//PATH:"/app:company_home/st:sites/cm:test/cm:documentLibrary/cm:abc/*"
Map params = new HashMap<>();
params.put("query", finalPath); 
NodeRef nodeRef = getNodelocatorService().getNode("xpath",null,params);

While doing the dependency injection for the nodelocatorService in your spring xml file, be careful you make the following entry: 
<property name="nodelocatorService" ref="nodeLocatorService"/>
instead of 
<property name="nodelocatorService" ref="NodeLocatorService"/>
Otherwise, you may get the following error. NoSuchBeanDefinitionException: No bean named 'NodeLocatorService' is defined

Also, you won't be able to use nodeLocatorService with fts-alfresco query.
This code - getNodelocatorService().getNode("fts-alfresco",null,params);
won't work. As for fts-alfresco nodeLocator is not defined in alfresco.
Courtesy: https://docs.alfresco.com/5.2/references/dev-servi...

You will get error like: 'No NodeLocator is registered with fts-alfresco' if you try with fts-alfresco.

2) Create a method like the following:
privateNodeRef getNode(String path){ 
logger.debug("Getting NodeRef for path:\""+ path +"\"");
ResultSet results =null;
try{
StoreRef storeRef =newStoreRef(StoreRef.PROTOCOL_WORKSPACE,"SpacesStore");
results = searchService.query(storeRef,SearchService.LANGUAGE_LUCENE,"PATH:\""+ path +"\"");
if(results.length()==0){ 
logger.debug("Zero matches for path: "+ path);
returnnull;
}
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();
}}}

3 comments:

  1. Best description ever..if you want to know about bit coin and getnode you can come in this link Getnode

    ReplyDelete
  2. Best newspaper ever you can read now https://bit.ly/3lZ5wkP

    ReplyDelete