Originally published at: ONLYOFFICE Docs Advanced API: Manage a comment feed for documents in one place | ONLYOFFICE Blog
In this article, you will learn how to generate a feed with comments from your documents using API to be able to navigate through comments, reply to comments and close them using your own buttons/methods instead of the editor buttons.
A brief intro
The newly added Connector API class allows interacting with text documents, spreadsheets, presentations, and fillable forms from the outside.
This way, you can create a special Connector which allows you to link your custom interface directly to the document and perform any actions in it.
Possible scenarios and cases include:
- managing a comment feed for all documents in one place;
- automatic form filling with the data exported from databases;
- managing review process from the outside (accepting/rejecting suggested changes);
- and more.
How to start
The Connector API class is included in the Advanced API and is available for new builds of ONLYOFFICE Docs Developer Edition starting from version 7.2.
So, first of all, you need to get a new license for ONLYOFFICE Docs Developer Edition with this option included (in the Access to API section select the Advanced option).
In case you want to try it first, start a new 30-day trial period of ONLYOFFICE Docs Developer Edition or request a demo by contacting our sales team at sales@onlyoffice.com. You are also welcome to reach your dedicated sales manager directly.
What you need to do
To implement a comment feed and work with comments using your own buttons/methods, you need to create the Connector. To do it, use the createConnector method of the document editor object:
var connector = docEditor.createConnector()
Here are the most important methods available for the Connector:
-
attachEvent allows adding an event listener previously available only from plugins. A full list is available here. Among them, you will find such events as changing the Content Control (
onChangeContentControl
) or clicking on the element (onClick
). - callCommand allows executing the Document Builder script to build and modify a document. More than 1,5 thousand methods are provided by the Builder Document script, and they all become available for executing in the Connector. A list of methods for documents, spreadsheets and presentations is available here.
-
executeMethod allows executing certain editor methods using the connector. These methods execute a set of complex Document Builder commands. And unlike pure Document Builder commands, they are connected with the focus position in the editor. For example, the
GetSelectedText
method allows retrieving the selected content, and theMoveToComment
method moves the focus to the comment.
Step-by-step implementation
Follow these actions and run the specified commands to manage comments in one place outside of the document.
Get all comments in your document:
connector.executeMethod("GetAllComments", null, function(comments) { /**/ })
Add a new comment to the element highlighted by the cursor:
connector.executeMethod("AddComment",[{Text: comment, UserName: userName, Time: datetime}]);
Modify any existing comment:
connector.executeMethod("ChangeComment", [commentId, newData]);
Delete any existing comment:
connector.executeMethod("RemoveComments", [commentId]);
Subscribe to the change comment event:
connector.attachEvent("onChangeCommentData", function (data) { /**/ })
Subscribe to the add comment event:
connector.attachEvent("onAddComment", function (comment) { /**/ })
Subscribe to the delete comment event:
connector.attachEvent("onRemoveComment", function (data) { /**/ })
This example shows that the new Advanced API provides multiple abilities for changing and controlling the document through your program code.
We are going to constantly extend the list of available methods and events. However, if you have a specific task or request but can’t find a suitable documented method for it, please let us know. You can contact your dedicated support or sales manager directly, or use the sales@onlyoffice.com email address.
Watch our webinar on API updates to learn more about the latest enhancements for developers: