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:
- One JS Script by using server JavaScript API (Alfresco Repository JavaScript API)
- Rules definition provided by Alfresco share web interface
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)
Rule definition
Once our script is available, just choose one site folder and create a new rule by using Run script option
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();
I’m pretty sure the email action will accept GROUP_ prefixed group authorities and it will resolve group membership.
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