cancel
Showing results for 
Search instead for 
Did you mean: 

Retrieve all properties belonging to a given aspect

leonardo_celati
Champ in-the-making
Champ in-the-making
I have searched a lot on this argument, but I couldn't find a proper solution. I mean, I can access easily all the properties, and iterate trough it, but I am not able to dinamically access only those properties related to a given aspect.

Supposing I have an aspect xy with properties xy:name and xy:surname, this code fragment should explain it all:


var properties = node.getAspectPropertiesOnly('xy:myAspect'); // <— just an example this method does not exists
for (key in properties) {
    logger.log(key + "=" + properties[key]);
    // do some operation…
}


Should output

<blockquote>
name = my name
surname = my surname
</blockquote>
4 REPLIES 4

romschn
Star Collaborator
Star Collaborator
AFAIK there isn't any direct API method to get the properties for aspect.
However, if you want to get all the properties for your specific aspect then, below could be one way to achieve this.
1. When you create the aspect in the content model, create a utility class having a method to which you will pass the node id as the parameter. In this method, using node service, fetch the properties defined under the aspect. You can have such methods for each of the aspects you have.
2. Based upon the aspect you are interested in to retrieve the property, invoke the relevant method from the utility class of your project.

You may want to take a look at the out-of-the-box code for AuditablePropertiesEntity.class's getAuditableProperties() method which returns the prperties for auditable aspect.

Hope this helps.

I was looking (hoping) for a generic method for all the aspects.

Anyway, the solution you are proposing sounds really interesting. I will include such utility methods in all my relevant custom aspects. Thanks.

s_palyukh
Star Contributor
Star Contributor
I believe the following code will help you:


var ctx = Packages.org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext();
var model = Packages.org.alfresco.model.ContentModel;
var dictionaryService = ctx.getBean("DictionaryService");
var myAspect = dictionaryService.getAspect(model.ASPECT_TITLED);
var props = myAspect.getProperties();
   
for (var i = 0; i < props.length; i++) {
   print("Prop Name = " + props.name);   
   
   print(node.properties[props.name]);
}


If you want to use you custom aspect then you need to create QName instead of using model.ASPECT_TITLED:


var ctx = Packages.org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext();
var dictionaryService = ctx.getBean("DictionaryService");
var namespaceService = ctx.getBean("NamespaceService");
var QName = Packages.org.alfresco.service.namespace.QName;
var qnameAspect = QName.createQName("xy:myAspect", namespaceService);

var myAspect = dictionaryService.getAspect(qnameAspect);
var props = myAspect.getProperties();

for (var i = 0; i < props.length; i++) {
   print("Prop Name = " + props.name);   

   print(node.properties[props.name]);
}

s_palyukh
Star Contributor
Star Contributor
I believe the following code will help you:


var ctx = Packages.org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext();
var model = Packages.org.alfresco.model.ContentModel;
var dictionaryService = ctx.getBean("DictionaryService");
var myAspect = dictionaryService.getAspect(model.ASPECT_TITLED);
var props = myAspect.getProperties();
   
for (var i = 0; i < props.length; i++) {
   print("Prop Name = " + props.name);   
   
   print(node.properties[props.name]);
}


If you want to use you custom aspect then you need to create QName instead of using model.ASPECT_TITLED:


var ctx = Packages.org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext();
var dictionaryService = ctx.getBean("DictionaryService");
var namespaceService = ctx.getBean("NamespaceService");
var QName = Packages.org.alfresco.service.namespace.QName;
var qnameAspect = QName.createQName("xy:myAspect", namespaceService);

var myAspect = dictionaryService.getAspect(qnameAspect);
var props = myAspect.getProperties();

for (var i = 0; i < props.length; i++) {
   print("Prop Name = " + props.name);   

   print(node.properties[props.name]);
}