Implementing Alfresco JavaScript API Functionalities

0
113
6 min read

 

Alfresco 3 Cookbook

Alfresco 3 Cookbook

Over 70 recipes for implementing the most important functionalities of Alfresco

        Read more about this book      

(For more resources on Alfresco, see here.)

The reader can benefit from the previous article on Alfresco 3: Writing and Executing Scripts.

 

Add/Change contents of a document

Let’s explore some example JavaScript. In the following example scripts, you will be able to witness the APIs and functionalities.

Getting ready

We will store the JavaScript files in the Company Home>Data Dictionary>Scripts>Cookbook folder (this folder does not exist in your repository and create this folder).

And will run the sample scripts against a document – Test_JS_API.txt in the folder Company Home>InfoAxon>Chapter 8. I have uploaded this text file with a simple line of text: A sample Document created to investigate in JavaScript API. and used our custom content type iabook:Product.

if (document.hasPermission("Write"))
{
if (document.mimetype == "text/plain")
{
if (!document.hasAspect("cm:versionable"))
document.addAspect("cm:versionable");
var wcopy = document.checkout();
var cnt = wcopy.content;
cnt += "rnThis line is added using the JavaScript.";
wcopy.content = cnt;
wcopy.checkin("Sample Line added via JS");
}
}

How to do it…

  1. Create a new script file in the Company Home>Data Dictionary>Scripts>Cookbook folder and save this code; let’s say the file is named changecontent.js
  2. Execute the script using Run Action on the document Test_JS_API.txt in the Chapter 8 folder.
  3. After running the script, a new version of the document will be created and a new line will be added in the document.

    Alfresco 3 Cookbook

  4. Thus each time you run the script for this document, a line will be appended at the end of the content and a new version will be created.

How it works…

The document object here automatically refers to the current document, in our case, it is Test_JS_API.txt, since we have executed the script against this document.

First we have checked whether we have proper permission to perform the write operation on the document. If the permission is there, we check the mimetype of the document, since the textual content writing operation is possible only for a few mimetypes such as text, html, and so on.

After that, we check whether the document is versionable or not, by default, any content you upload in the repository is not versionable. So we add the cm:versionable aspect in case it is not there already.

Then we checkout the document and append the line of text we want in the working copy. After updating the content, we checking the working copy with a commit comment. This comment is visible in the Version History of the document.

Though it is not always mandatory to check for the required permissions, it is a good practice to confirm for the relevant permissions, otherwise Alfresco may throw runtime errors in case the required permissions are not available.

 

Creating a backup copy of a document

In this recipe, we will write a script to create a backup copy of a particular document.

How to do it…

  1. Create a new script file in the Company Home>Data Dictionary>Scripts>Cookbook folder and add the following code. Let’s say the file is named createbackup.js

    var back = space.childByNamePath("Backup");
    if (back == null && space.hasPermission("CreateChildren"))
    {
    back = space.createFolder("Backup");
    }

    if (back != null && back.hasPermission("CreateChildren"))
    {
    var copied = document.copy(back);
    if (copied != null)
    {
    var backName = "Backup of " + copied.name;
    copied.name = backName;
    copied.properties.description = "This is a Backup copy created
    by JS";
    copied.save();
    }
    }

  2. Execute the script using Run Action on the document Test_JS_API.txt in the Chapter 8 folder.
  3. After executing the script, a new folder named Backup will be created (if it does not exist already) and a copy of this document (named Backup of Test_JS_API.txt) will be created in the backup folder.

(Move the mouse over the image to enlarge.)

How it works…

The space object here automatically refers to the current space. In our case, it is Chapter 8, since we have executed the script against a document from this folder.

The document object here automatically refers to the current document. In our case, it is Test_JS_API.txt, since we have executed the script against this document.

First we have checked whether a space already exists there with the name Backup under Chapter 8. If not, we create the space. This is the space where we intend to create our backup copy.

After that, we check whether we have the proper permission to create a new document in the backup folder. We do this by checking the CreateChildren permission.

If we have the proper required permission, we create a copy of the document in the backup folder. Then we change a few properties of the copied document – we change the name and description, for instance. After changing the properties, we save the changes.

Note that you do not need to save after changing the content of a document. However, you need to do this in case you change any property of the content item.

 

Adding a tag to a document

In this recipe, we will write a script that can be used to tag a document.

How to do it…

  1. Create a new script file in the Company Home>Data Dictionary>Scripts>Cookbook folder and add the following code; let’s say the file is named addtag.js

    if (!document.hasAspect("cm:taggable"))
    document.addAspect("cm:taggable");

    document.addTag("test");

  2. Execute the script using Run Action on the document Test_JS_API.txt in the Chapter 8 folder.
  3. The document will not be taggable, and a new tag has been added with the document – test. This is reflected in the property sheet of the document.

    Alfresco 3 Cookbook

  4. Now, you can also add more tags using the property editor dialog.

Alfresco 3 Cookbook

How it works…

The code we presented is rather simple in this case. As usual, the document object here automatically refers to the current document. In our case, it is Test_JS_API.txt, since we have executed the script against this document.

First we have checked whether the document already has the cm:taggable aspect associated with it, if not we add this aspect.

Then it is just about adding a tag – we added a tag test.

You can also add multiple tags at a time using the addTags method (we have used the addTag method to add a single tag in our example).

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here