10-25-2018 12:25 PM
Hi. I have to add a custom aspect to the default type cmerson in Alfresco 5.2. How can I do this?
10-26-2018 05:05 AM
I was adding my custom aspect to alfresco node comments. In Share, if you open a document to edit, then you will see the comments section on the bottom, through which you can add/edit/delete one or more comments.
From this I believe we could add custom aspects to any node.
There are Rest API calls that could add (our custom) aspect to an (already) existing node. I think there is a node id for your object/document. Once you know this, then below is some code extract...
This works in 5.2 (community) and above. I am not sure how much of this Rest APIs are available for 5.1 or earlier.
Please note that there could be many more ways to accomplish what you want. I know only this way.
For example, if you use CMIS api, there is a way to attach an aspect while document creation itself. I had attached to our custom (document) type, not to the default type. Well, I wonder why not attach an aspect to the native/default types, as long as the aspect is defined and deployed to the repository.
Anyways, following is one way to do it through Rest API on an existing document (node). For this, your document had to be created already, and it has the node_id therefore.
restApiAccessPoint is "/alfresco/api/-default-/public/alfresco/versions/1"
nodeId is the id of the document, in my case it was the id of the comment object.
String jsonString = "{\"aspectNames\":[\"sloperationInfo\"],\"properties\": {\"slo:lo_operation_user\":\"" + loUser + "\",\"slo:lo_operation_info\":\"Added\"}}";
restUrl = new URL ( (this.repoHost + this.restApiAccessPoint + "/nodes/" + nodeId) );
HttpPut putFunction = new HttpPut(restUrl.toString());
try {
StringEntity entity = new StringEntity(jsonString);
putFunction.setEntity(entity);
putFunction.setHeader("Accept", "application/json");
putFunction.setHeader("Content-type", "application/json");
response = httpClient.execute(putFunction);
int status_code = response.getStatusLine().getStatusCode();
if (status_code != 200) {
}
}
catch....
Here is my custom aspect definition that is deployed to the repo (using AMP). (names slightly changed for privacy)
<aspect name="sloperationInfo">
<title>LO operation information</title>
<properties>
<property name="slo:lo_operation_date">
<title>Date of operation</title>
<type>d:datetime</type>
</property>
<property name="slo:lo_operation_user">
<title>LO User</title>
<type>d:text</type>
</property>
<property name="slo:lo_operation_info">
<title>Operation</title>
<type>d:text</type>
</property>
</properties>
</aspect>
10-28-2018 10:14 AM
Yes, you can add custom aspects to nodes that are instances of out-of-the-box types.
You can investigate using the REST API as was suggested. The easiest way to do it, in my opinion, is via the JavaScript Console. (If you have not already installed the JavaScript Console you really should. It is very helpful).
If you want to add aspects automatically to nodes when they are created, you can use a rule on a folder. This works great for instances of cm:folder and cm:content.
If you want to add aspects automatically to instances of cmerson, you might want to investigate writing a behavior that is bound to the cmerson type.
You can learn more about custom types, custom aspects, and behaviors and other topics in my tutorials: https://ecmarchitect.com/alfresco-developer-series
Explore our Alfresco products with the links below. Use labels to filter content by product module.