Adding an aspect to Alfresco node comments?

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Established Member

Adding an aspect to Alfresco node comments?

Jump to solution

Background

We are using node/alfresco comments feature.

What is the comment feature that I am referring to?

If we go to Share UI, and go to edit a document, we will see comments way below. There, we can add, edit or delete comments of a document.

For now, our application communicates with (or accesses documents in) Alfresco repository through CMIS and Rest API. We use CMIS API for all the regular document access.

Since CMIS API does not support the "alfresco comments feature", we use Rest API to access some existing web-scripts to access the document comments.

Requirement

Our requirement is to attach our application's (native) user info on the document comments. This would tell us which of our application user added a comment on the document.

Approach?

From recommendations by some experts here in this forum, I have created a custom aspect with the user and date property. This aspect is deployed and available to access (through code I believe).

Experts recommended me to write a special web-script that adds (or attaches) this aspect to a comment node. Then the plan is to access this web-script through Rest API.

While learning web scripts, I have practiced writing some simple "hello world" kind of web-scripts using Javascript.

Also, we have Alfresco 5.2 community.

Question

It looks like we have Javascript or Java-backed web-scripts. Having a hard time figuring the choice.

Do you have (online) reference to any sample (web-script) code that adds an aspect to a node or to a comment node?

Any guidance is appreciated.

1 Solution

Accepted Solutions
Highlighted
Professional

Re: Adding an aspect to Alfresco node comments?

Jump to solution

It looks like you may have decided to use the REST API to add the aspect to your comment node based on this question: Adding an aspect to a comment node has error 

If you haven't already, you should read my tutorial on Web Scripts. It includes an example that uses the NodeService to add an aspect to a node with an addAspect method call.

If you decide to use a JavaScript controller you should read the JavaScript API documentation. Here is one of the docs that discusses addAspect.

Regarding whether you use a Java or a JavaScript controller, it really doesn't matter much. You should probably use whichever you are more comfortable developing with.

You might some day find API's that are easier to work with in one versus the other, but simple stuff like addAspect is well-covered in both Java and JavaScript.

View solution in original post

2 Replies
Highlighted
Professional

Re: Adding an aspect to Alfresco node comments?

Jump to solution

It looks like you may have decided to use the REST API to add the aspect to your comment node based on this question: Adding an aspect to a comment node has error 

If you haven't already, you should read my tutorial on Web Scripts. It includes an example that uses the NodeService to add an aspect to a node with an addAspect method call.

If you decide to use a JavaScript controller you should read the JavaScript API documentation. Here is one of the docs that discusses addAspect.

Regarding whether you use a Java or a JavaScript controller, it really doesn't matter much. You should probably use whichever you are more comfortable developing with.

You might some day find API's that are easier to work with in one versus the other, but simple stuff like addAspect is well-covered in both Java and JavaScript.

View solution in original post

Highlighted
Established Member

Re: Adding an aspect to Alfresco node comments?

Jump to solution

Thank you very much for giving pointers on the Web Scripts and JavaScript API. Sometimes, in the crunch of time based work, it is hard to search online and land on a good article or resource. 

Yes, I went ahead with the "Alfresco Content Services REST API" (not the web-script one) to add the aspect to the comment. This is the method. This value c6c9e3af-6e0b-46ad-8e11-648e1a27e7e6 is the comment node id. The comment must have been created already using a prior Rest API call.


HTTP PUT (for attaching aspect with its value to a comment node)
<AlfrescoServerPort>/alfresco/api/-default-/public/alfresco/versions/1/nodes/c6c9e3af-6e0b-46ad-8e11-648e1a27e7e6

HTTP Body
{
  "aspectNames":[
    "sloSmiley SurprisedperationInfo"
  ],
  "properties": {
    "sloSmiley Surprisedperation_user":"Goose",
    "sloSmiley Surprisedperation_info":"A comment is added"
  }
}

In my code, I do like shown below. The variable restApiUrl contains the PUT url as given above, and the variable jsonString contains the above HttpBody Json content.

CloseableHttpResponse response = null;
URL restApiUrl = null;

CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(this.configUser, this.configUserPassword);
provider.setCredentials(AuthScope.ANY, creds);
CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();

HttpPut putFunction = new HttpPut(restApiUrl.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);

}
catch (Exception e) {}
finally {
if (response != null) { try { response.close(); } catch (Exception ignore) {} }
}

As you have mentioned before, I see that this is a two call approach. First there is a Rest API call to create the comment (node). Then we have a Rest API call to attach/create the custom aspect.

For now, since this is purely user (navigation) action driven, there is only a remote possibility that it would pose scaling problem. 

In future version of our application, I would certainly consider a Web Script approach, where I can achieve all this in a single call.