Alfresco – Sending a notification email to Site Administrators by using JavaScript API

It occurs that we developers like to use Alfresco SDK to customize Alfresco in every way. However Alfresco provides a wide catalog of techniques to develop our projects and every technique is more or less suitable for every purpose. In this article, we are going to show how to send an email to every Site Administrator every time a document is uploaded to a folder.

Following tools are going to be used:

Script development

This script will be executed in the scope of a Site folder, so we are going to get access to required root objects in order to get Site Administrators list and their emails.

sendMailSiteManagers.js


function sendMail() {

    var site = siteService.getSite(document.siteShortName);
    var members = site.listMembers(null, "SiteManager", 0, false);

    for (userName in members) {

        var person = people.getPerson(userName);
        var email = person.properties["cm:email"];

        var mail = actions.create("mail");
        mail.parameters.to = email;
        mail.parameters.subject = "New document available: " + document.name;
        mail.parameters.template =
            companyhome.childByNamePath("Data Dictionary/Email Templates/Notify Email Templates/notify_user_email.html.ftl");
        mail.execute(document);

    }

}
sendMail();

In order to have this script available in web interface, we need to upload it to scripts folder repository (Data Dictionary > Scripts)

alfresco-js-deploy-folder

Rule definition

Once our script is available, just choose one site folder and create a new rule by using Run script option

alfresco-share-define-rule

 

From now, every Site Administrator will receive a new mail (based on notify_user_email.html.ftl email template) every time a new document is uploaded to this folder.

No special technical abilty is required to customize an Alfresco instance. Everyone have chance to adapt the product to their requirements.

Edited

As Bindu points out at the comments, this script can be simplified by using to_many mail parameter.

function sendMail()
{

    var mail = actions.create("mail");
    mail.parameters.to_many = "GROUP_site_" + document.siteShortName + "_SiteManager";
        mail.parameters.subject = "New document available: " + document.name;
        mail.parameters.template =
            companyhome.childByNamePath("Data Dictionary/Email Templates/Notify Email Templates/notify_user_email.html.ftl");

    mail.execute(document);

}
sendMail();

3 comentarios en “Alfresco – Sending a notification email to Site Administrators by using JavaScript API

  1. Pingback: Alfresco – Customizing alfresco-unzip-action addon | Programming and So

  2. Good point Bindu!

    You can include something like mail.parameters.to = "GROUP_SiteManager_" + (document.siteShortName). The main difference is article source code will send one mail for every Site Manager, using your suggested alternative one mail will be sent for all Site Administrators.

    I’m thinking that maybe mail.parameters.to_many should be used… Anyway it can be done!

    Thanks

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s