Alfresco 3: Web Scripts

Exclusive offer: get 50% off this eBook here
Alfresco 3 Cookbook

Alfresco 3 Cookbook — Save 50%

Over 70 recipes for implementing the most important functionalities of Alfresco

£22.99    £11.50
by Snig Bhaumik | July 2011 | Open Source

Alfresco Web Scripts provide RESTful APIs of the repository services and functions. The article elaborates all related concepts, knowledge and how-to do it recipes that would help you write, deploy and debug web scripts.

In this article Snig Bhaumik, author of Alfresco 3 Cookbook, we will cover:

  • Writing and deploying Web Scripts
  • Displaying details of documents
  • Sending e-mails using a Mail Template

 

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.)

Introduction

You all know about Web Services – which took the web development world by storm a few years ago. Web Services have been instrumental in constructing Web APIs (Application Programming Interface) and making the web applications work as Service-Oriented Architecture. In the new Web 2.0 world, however, many criticisms arose around traditional Web Services – thus RESTful services came into the picture. REST (Representational State Transfer) attempts to expose the APIs using HTTP or similar protocol and interfaces using well-known, light-weight and standard methods such as GET, POST, PUT, DELETE, and so on.

Alfresco Web Scripts provide RESTful APIs of the repository services and functions. Traditionally, ECM systems have been exposing the interfaces using RPC (Remote Procedure Call) – but gradually it turned out that RPC-based APIs are not particularly suitable in the wide Internet arena where multiple environments and technologies reside together and talk seamlessly. In the case of Web Scripts, the RESTful services overcome all these problems and integration with an ECM repository has never been so easy and secure. Alfresco Web Scripts were introduced in 2006 and since then it has been quite popular with the developer and system integrator community for implementing services on top of the Alfresco repository and to amalgamate Alfresco with any other system.

What is a Web Script?

A Web Script is simply a URI bound to a service using standard HTTP methods such as GET, POST, PUT, or DELETE. Web Scripts can be written using simply the Alfresco JavaScript APIs and Freemarker templates, and optionally Java API as well with or without any Freemarker template.

For example, the http://localhost:8080/alfresco/service/api/search/person.html ?q=admin&p=1&c=10 URL will invoke the search service and return the output in HTML.

Internally, a script has been written using JavaScript API (or Java API) that performs the search and a FreeMarker template is written to render the search output in a structured HTML format.

All the Web Scripts are exposed as services and are generally prefixed with http://<<server-url>>/<<context-path>>/<<servicepath>>. In a standard scenario, this is http://localhost:8080/alfresco/service

Web Script architecture

Alfresco Web Scripts strictly follow the MVC architecture.

Alfresco 3 Cookbook

  • Controller: Written using Alfresco Java or JavaScript API, you implement your business requirements for the Web Script in this layer. You also prepare your data model that is returned to the view layer. The controller code interacts with the repository via the APIs and other services and processes the business implementations.
  • View: Written using Freemarker templates, you implement exactly what you want to return in your Web Script. For data Web Scripts you construct your JSON or XML data using the template; and for presentation Web Scripts you build your output HTML. The view can be implemented using Freemarker templates, or using Java-backed Web Script classes.
  • Model: Normally constructed in the controller layer (in Java or JavaScript), these values are automatically available in the view layer.

Types of Web Scripts

Depending on the purpose and output, Web Scripts can be categorized in two types:

  1. Data Web Scripts: These Web Scripts mostly return data in plenty after processing of business requirements. Such Web Scripts are mostly used to retrieve, update, and create content in the repository or query the repository.
  2. Presentation Web Scripts: When you want to build a user interface using Web Scripts, you use these Web Scripts. They mostly return HTML output. Such Web Scripts are mostly used for creating dashlets in Alfresco Explorer or Alfresco Share or for creating JSR-168 portlets.

Note that this categorization of Web Script is not technically different—it is just a logical separation. This means data Web Scripts and presentation Web Scripts are not technically dissimilar, only usage and purpose is different.

Web Script files

Defining and creating a Web Script in Alfresco requires creating certain files in particular folders. These files are:

  1. Web Script Descriptor: The descriptor is an XML file used to define the Web Script – the name of the script, the URL(s) on which the script can be invoked, the authentication mechanism of the script and so on. The name of the descriptor file should be of the form: <<service-id>>.<<http-method>>. desc.xml; for example, helloworld.get.desc.xml.
  2. Freemarker Template Response file(s) optional: The Freemarker Template output file(s) is the FTL file which is returned as the result of the Web Script. The name of the template files should be of the form: &lt;<service-id>>.<<httpmethod>>.<< response-format>>.ftl; for example, helloworld.get.html.ftl and helloworld.get.json.ftl.
  3. Controller JavaScript file (optional): The Controller JavaScript file is the business layer of your Web Script. The name of the JavaScript file should be of the form: <<service-id>>.<<http-method>>.js; for example, helloworld.get.js.
  4. Controller Java file (optional): You can write your business implementations in Java classes as well, instead of using JavaScript API.
  5. Configuration file (optional): You can optionally include a configuration XML file. The name of the file should be of the form: <<service-id>>.<<http-method>>.config.xml; for example, helloworld.get.config.js.
  6. Resource Bundle file (optional): These are standard message bundle files that can be used for making Web Script responses localized. The name of message files would be of the form: <<service-id>>.<<http-method>>.properties; for example, helloworld.get.properties.

The naming conventions of Web Script files are fixed – they follow particular semantics.

Alfresco, by default, has provided a quite rich list of built-in Web Scripts which can be found in the \tomcat\webapps\alfresco\WEB-INF\classes\alfresco\templates\webscripts\org\alfresco folder. There are a few locations where you can store your Web Scripts.

  1. Classpath folder: \tomcat\webapps\alfresco\WEB-INF\classes\alfresco\templates\webscripts\
  2. Classpath folder (extension): \tomcat\webapps\alfresco\WEB-INF\classes\alfresco\extension\templates\webscripts\
  3. Repository folder: /Company Home/Data Dictionary/Web Scripts
  4. Repository folder (extension): /Company Home/Data Dictionary/Web Scripts Extensions

It is not advised to keep your Web Scripts in the org\alfresco folder; this folder is reserved for Alfresco default Web Scripts. Create your own folders instead. Or better, you should create your Web Scripts in the extension folders.

Web Script parameters

You of course need to pass some parameters to your Web Script and execute your business implementations around that.

You can pass parameters by query string for the GET Web Scripts. For example:

http://localhost:8080/alfresco/service/api/search/person.html?q=admin&p=1&c=10

In this script, we have passed three parameters – q (for the search query), p (for the page index), and c (for the number of items per page). You can also pass parameters bound in HTML form data in the case of POST Web Scripts. One example of such Web Script is to upload a file using Web Script.

 

Alfresco 3 Cookbook Over 70 recipes for implementing the most important functionalities of Alfresco
Published: July 2011
eBook Price: £22.99
Book Price: £36.99
See more
Select your format and quantity:

 

        Read more about this book      

(For more resources on Alfresco, see here.)

Writing and deploying Web Scripts

As you know, writing a Web Script requires creating some files in certain locations. Here in this recipe we will see how this can be done. The best part is you do not require any Java development environment; no Eclipse is required for writing Web Scripts, no Java programming knowledge is essential. However, if you wish to create Java-backed Web Scripts, you may need these.

In the latter examples and illustrations, we will create our Web Scripts in the classpath folder in a new package, com\infoaxon. Let's say we will create a Web Script with the service-ID showhomedetails.

We expect the Web Script to return the contents of the current user's home space in a structured HTML format.

How to do it...

  1. First we will create the folder where the Web Script files will be stored. Create a folder named com under \tomcat\webapps\alfresco\WEB-INF\classes\alfresco\templates\webscripts\, create the folder infoaxon under this. Thus we will be creating our Web Script files under the folder \tomcat\webapps\alfresco\WEB-INF\classes\alfresco\templates\webscripts\com\infoaxon.
  2. Next we need to write the Web Script descriptor. Open your favorite text editor (or you can use any XML editor such as oXygen).
  3. Enter the following text:

    <webscript>
    <shortname>Home Space Contents</shortname>
    <description>Retrieves and displays all contents under current
    user's Home Space.</description>
    <url>/showhomedetails</url>
    <format default="html"/>
    <authentication>user</authentication>
    <transaction>required</transaction>
    </webscript>

  4. Save this file with the name showhomedetails.get.desc.xml.
  5. Then we will create the response Freemarker template that will return the output HTML.
  6. Open a new text file and enter the following text:

    <html>
    <body>
    <#if userhome?exists>
    <h2>Sub Folders</h2>
    <table cellpadding="5" cellspacing="5" border="1">
    <tr style="font-weight: bold"><td>Name</td><td>Create Date</
    td><td>Creator</td><td>Offspring</td></tr>
    <#list userhome.children as c>
    <#if c.isContainer>
    <tr><td>${c.name}</td><td>${c.properties["cm:created"]
    ?datetime}</td><td>${c.properties["cm:creator"]}</td><td>${c.
    children?size}</td></tr>
    </#if>
    </#list>
    </table>
    <h2>Content Items</h2>
    <table cellpadding="5" cellspacing="5" border="1">
    <tr style="font-weight: bold"><td>Name</td><td>Create Date</
    td><td>Creator</td><td>Size (bytes)</td></tr>
    <#list userhome.children as c>
    <#if c.isDocument>
    <tr><td>${c.name}</td><td>${c.properties["cm:created"]?da
    tetime}</td><td>${c.properties["cm:creator"]}</td><td>${c.size}</
    td></tr>
    </#if>
    </#list>
    </table>
    <#else>
    <h3>User Home not Found!</h3>
    </#if>
    </body>
    </html>

  7. That's all – you have created your first Web Script. Restart your server and navigate to http://localhost:8080/alfresco/service/showhomedetails (this is our Web Script URL). The system will ask for user credentials and you will be able to see some similar HTML output, as shown below (I am logged in as the admin user).

Alfresco 3 Cookbook

It is not that you always have to restart your server after creating a new Web Script or updating an existing one. You can refresh the Web Script library as well.

How it works...

We have assigned the URL of the service as showhomedetails. Thus, the full URL of the service becomes http://localhost:8080/alfresco/service/showhomedetails.

As the authentication option, we have given <code>user</code>. There are four authentication options available for Web Scripts to run. They are as follows:

  1. none: Means no authentication required. Alfresco will not ask for any user credentials while running the script. However, most of the repository functionalities and services will be unavailable in this case.
  2. guest: Means guest authentication required. Alfresco will not ask for any user credentials yet again, but will assume the guest permissions. Again, many of the repository functionalities and services will be unavailable in this case, since the guest user is not allowed to access most of the repository assets.
  3. user: Means a valid user credential is required for running the script. Alfresco thus will ask for the username and password for running the script.
  4. admin: Means only administrators can access the Web Script. You normally use this setting in case of any administrative jobs such as managing users, groups, and so on.

We want to return HTML output from the Web Script, thus we have created an HTML template file. The code of the template is pretty simple – just displaying the contents of the current user's home folder in two HTML tables.

You will notice that we have not created any controller script for this service. This is because for now we did not feel any need for that. However, we can restructure the code and make use of the controller script.

  1. Create a new text file. Put the following code:

    function main()
    {
    var folders = new Array();
    var docs = new Array();
    if (userhome != null)
    {
    var children = userhome.children;
    for (i=0; i<children.length; i++)
    {
    var c = children[i];
    if (c.isContainer) folders.push(c);
    else if (c.isDocuent) docs.push(c);
    }
    }
    model.folders = folders;
    model.docs = docs;
    }
    main();

  2. Save the file as showhomedetails.get.js in the same folder.

    The userhome object we have used here in this script is one of the root-level objects.

  3. The following is the updated template code. Put the following text in the showhomedetails.get.html.ftl file:

    <html>
    <body>
    <#if folders?exists>
    <h2>Sub Folders</h2>
    <table cellpadding="5" cellspacing="5" border="1">
    <tr style="font-weight: bold"><td>Name</td><td>Create Date</
    td><td>Creator</td><td>Offspring</td></tr>
    <#list folders as c>
    <tr><td>${c.name}</td><td>${c.properties["cm:created"]?d
    atetime}</td><td>${c.properties["cm:creator"]}</td><td>${c.
    children?size}</td></tr>
    </#list>
    </table>
    </#if>
    <#if docs?exists>
    <h2>Content Items</h2>
    <table cellpadding="5" cellspacing="5" border="1">
    <tr style="font-weight: bold"><td>Name</td><td>Create Date</
    td><td>Creator</td><td>Size (bytes)</td></tr>
    <#list docs as c>
    <tr><td>${c.name}</td><td>${c.properties["cm:created"]?dateti
    me}</td><td>${c.properties["cm:creator"]}</td><td>${c.size}</td>
    </tr>
    </#list>
    </table>
    </#if>
    </body>
    </html>

  4. Run the Web Script and you will see the same HTML output.

One important object here is the model object that is the bridge between the model and the view and it is available as a root object in the scope of the script. Thus whatever variables and values you assign into this object will be available in the template view.
For example, we have assigned two variables, folders and docs, in the model object – these are available in the FreeMarker template. You can use these values in the view and display the values as per your requirement.

There's more…

We have used several FreeMarker syntaxes in this script.

  1. The <#list...> tag is used to iterate through an array of objects – just like a for each loop in any other programming language.
  2. For example, in <#list folders as c>, we have iterated through the folders array and each element of the array is represented by c.
  3. The ScriptNode object is available in the Web Script FreeMarker template as well. Thus you will be able to extract and use any of the properties of this object.
  4. For example, by the statement c.isContainer – we have evaluated whether the current ScriptNode is a space or not. Similarly, c.properties["cm:creator"] returns the creator property value of the object. In this way, you can extract any available property of the current object.

 

Using the Alfresco Web Script browser

The Web Script browser is an interface provided by Alfresco explorer that helps you browse, view, manage, and run all the deployed Web Scripts.

This interface is available at http://localhost:8080/alfresco/service/index, assuming you are running Alfresco Tomcat at the localhost 8080 port.

You can use this interface for quite a number of purposes.

Alfresco 3 Cookbook

  • Browse and see all the Web Scripts deployed and configured
  • Browse Web Scripts by URIs
  • Browse Web Scripts by packages

Alfresco 3 Cookbook

  1. This is a partial output of the listing of Web Scripts by packages. You can notice that our new package /com/infoaxon is also registered there.
    You can see and validate all details of a particular Web Script by this interface. Let's see how we can view the details of our new script we have just created in the previous recipe.
    1. Click on Browse by Web Script URI.
    2. Look for our new script with the URI /showhomedetails.
    3. Brief details of the Web Script will appear as follows:

    Alfresco 3 Cookbook

  2. You can see that most of the details in this page are appearing from the descriptor XML file you can create for our script.
  3. Click on the GET /alfresco/service/showhomedetails link to execute the script.
  4. Click on the Id link (the com/infoaxon/showhomedetails.get link) to view the detailed information of the Web Script. This interface will render the full code and implementation set of the Web Script.

    Alfresco 3 Cookbook

    • This is again a partial output of this page. You can see that almost all the information you need for a Web Script is presented in this page.
    • The last link (description link) is to show the descriptor XML file.
  5. Finally, using the Refresh Web Scripts button, you can reload the library of the Web Script.

Thus there is no need to restart the server each time you change a script or create a new one. Simply use this button and the Web Scripts will be refreshed.

The refresh operation is required if you change in the controller scripts or in the descriptor file. Changes in the templates do not require refreshing the Web Scripts.

If you feel your Web Script changes have not taken effect even after refreshing the scripts, check the Web Script detailed page to make sure that your code has been updated.

Now that we have understood the basics of Web Scripts and know how the Web Scripts can be created and deployed, let's begin creating some commonly used and example Web Scripts.

The next few recipes will demonstrate some sample Web Scripts.

 

Alfresco 3 Cookbook Over 70 recipes for implementing the most important functionalities of Alfresco
Published: July 2011
eBook Price: £22.99
Book Price: £36.99
See more
Select your format and quantity:

 

        Read more about this book      

(For more resources on Alfresco, see here.)

Displaying details of documents

This Web Script will accept a string from a user, will search for documents with that name, and finally will display the details of all those documents.

How to do it…

  1. We will have to create new files for the Web Script, let's name this script as documentdetails.
  2. Open a text editor and create a new file with this code:

    <webscript>
    <shortname>Document Details</shortname>
    <description>Searches document(s) and displays details of all
    the documents.</description>
    <url>/com/infoaxon/docdetails</url>
    <format default="html"/>
    <authentication>user</authentication>
    <transaction>required</transaction>
    </webscript>

  3. Save the file with the name docdetails.get.desc.xml in the \tomcat\webapps\alfresco\WEB-INF\classes\alfresco\templates\webscripts\com\infoaxon folder. This is our Web Script descriptor file.
  4. Now we create the JavaScript controller file. Here is the code:

    function main()
    {
    var dname = args.dname;
    var qry = "@cm\\:name:*" + dname + "*";
    var docs = search.luceneSearch(qry);
    model.docs = docs;
    model.dname = dname;
    }
    main();

  5. Save this file with the name docdetails.get.js in the same folder (all our Web Script files will be saved in this folder).
  6. Finally we create the template file:

    <html>
    <body>
    <#if dname?exists>
    <h2>Found Documents with name: ${dname}</h2>
    <#if docs?exists>
    <table cellpadding="5" cellspacing="5" border="1">
    <tr style="font-weight: bold"><td>Name</td><td>Create Date</
    td><td>Creator</td><td>Size (bytes)</td></tr>
    <#list docs as c>
    <tr><td>${c.name}</td><td>${c.properties["cm:created"]?da
    tetime}</td><td>${c.properties["cm:creator"]}</td><td>${c.size}</
    td></tr>
    </#list>
    </table>
    </#if>
    <#else>
    <h2>Search Parameter not provided.</h2>
    </#if>
    </body>
    </html>

  7. Save this file with the name docdetails.get.html.ftl.
  8. Refresh your Web Script list. The Web Script should now be deployed. Now let's run the script.
  9. We have assigned the URL of the script as /com/infoaxon/docdetails – thus the full URL would be http://localhost:8080/alfresco/service/com/infoaxon/docdetails. If you access this URL, the output would be as shown in the following screenshot:

    (Move the mouse over the image to enlarge.)

  10. That means we have not provided the name of the document to search for. We provide this parameter in this way: http://localhost:8080/alfresco/service/com/infoaxon/docdetails?dname=infoaxon – just like a standard querystring in an HTTP GET call. Depending on the contents in your repository, now the output would be as shown in the following screenshot:

  11. You can change the querystring dname value to see the corresponding outputs.

How it works…

One of the most important things to be noticed here is how we retrieve the querystring arguments passed in the Web Script. For that, a root-level object args is provided that returns a JavaScript array of all parameters passed.

We have used the dname parameter here. In this way, you can pass any number of parameters and use them in the script or template.

We then search for documents which have the keyword anywhere in the name. We have used the Lucene query "@cm\\:name:*" + dname + "*". When we pass infoaxon as the dname value, the Lucene query would be "@cm\\:name:*infoaxon*".

So the search operation is looking for documents with infoaxon anywhere in the name. The search output is returned to the template and the template renders the HTML view of the documents. Now, suppose we want to return a JSON string instead of HTML of the same Web Script. For that, we create another template file named docdetails.get.json.ftl and put the following code:

<#if docs?exists>
[
<#list docs as c>
{
"Name":"${c.name}",
"Create Date":"${c.properties["cm:created"]?datetime}",
"Creator":"${c.properties["cm:creator"]}",
"Size":"${c.size}"
}
<#if c_has_next>,</#if>
</#list>
]
<#else>
"OutputMessage":"${outputmessage}"
</#if>

We invoke this via this URL http://localhost:8080/alfresco/service/com/infoaxon/docdetails.json?dname=infoaxon instead of http://localhost:8080/alfresco/service/com/infoaxon/ docdetails?dname=infoaxon.

As you can see, you can call different response templates by putting the extension in the Web Script URL itself.

In this example, the default response format is HTML (see the Web Script descriptor file). Thus, when no format is provided, by default, HTML is returned. However, for JSON output, we had to manually put the expected response format.

However, you can invoke this by the http://localhost:8080/alfresco/service/com/infoaxon/docdetails?format=json&dname=cookbook URL as well. This will also render the same output.

[
{
"Name":"infoaxon",
"Create Date":"Aug 30, 2010 1:00:41 PM",
"Creator":"admin",
"Size":"0"
}
,
{
"Name":"InfoAxon",
"Create Date":"Jun 25, 2010 5:22:40 PM",
"Creator":"admin",
"Size":"0"
}
,
{
"Name":"Searching InfoAxon",
"Create Date":"Sep 28, 2010 4:01:09 PM",
"Creator":"admin",
"Size":"482"
}
,
{
"Name":"InfoAxon Solutions",
"Create Date":"Oct 27, 2010 1:02:24 PM",
"Creator":"admin",
"Size":"0"
}
,
{
"Name":"Infoaxon.doc",
"Create Date":"Dec 15, 2010 1:01:05 PM",
"Creator":"admin",
"Size":"28,160"
}
,
{
"Name":"Infoaxon.pdf",
"Create Date":"Dec 15, 2010 1:06:16 PM",
"Creator":"admin",
"Size":"59,931"
}
,
{
"Name":"About InfoAxon.html",
"Create Date":"Aug 24, 2010 11:05:58 AM",
"Creator":"admin",
"Size":"896"
}
,
{
"Name":"InfoAxon_Data.acp",
"Create Date":"Oct 27, 2010 4:18:05 PM",
"Creator":"admin",
"Size":"1,255,309"
}
,
{
"Name":"About InfoAxon.html discussion",
"Create Date":"Oct 28, 2010 4:16:57 PM",
"Creator":"admin",
"Size":"0"
}
,
{
"Name":"About InfoAxon.html",
"Create Date":"Aug 30, 2010 11:59:51 AM",
"Creator":"admin",
"Size":"1,019"
}
]

It is good practice to validate the output JSON string from your Web Script by a JSON validator service. I normally use JSONLint – http://www.jsonlint.com/

There's more…

To search for contents, we have used the search root level object in the JavaScript here.

 

Sending e-mails using a mail template

In this recipe, we will create a Web Script that will send a mail to a particular defined user. The content of the mail will be driven by a mail template. Finally, the mail content is returned as the Web Script response.

How to do it…

  1. We will first create a Mail Template. Here is the template:
    Hello ${person.properties["cm:firstName"]},
    Welcome to Alfresco!
    Here are your registration details.
    First Name: ${person.properties["cm:firstName"]}
    Last Name: ${person.properties["cm:lastName"]}
    User Name: ${person.properties["cm:userName"]}
    Enjoy your Stay!
    Regards,
    the Alfresco Team.
  2. Save this template in the Company Home | Data Dictionary | EMail Templates folder – with the name userdetailsmail.ftl
  3. Now, we will create new files for the web script; let's name this script as sendusermail.
  4. Open a text editor and create a new file with this code:

    <webscript>
    <shortname>Send Mail</shortname>
    <description>Searches for a user and sends mail to him about
    his details.</description>
    <url>/com/infoaxon/sendusermail</url>
    <format default="html"/>
    <authentication>admin</authentication>
    <transaction>required</transaction>
    </webscript>

  5. This is the Web Script descriptor files, save this with the name sendusermail.get.desc.xml ( in the same folder mentioned earlier).
  6. We will now create the controller JS file. Here is the code:

    function main()
    {
    if (args.uname == null)
    {
    model.result = "No user";
    return;
    }
    var usr = people.getPerson(args.uname);
    if (usr == null)
    {
    model.result = "No user";
    return;
    }
    var mail = actions.create("mail");
    mail.parameters.to = usr.properties["cm:email"];
    mail.parameters.subject = "Welcome!";
    mail.parameters.from = "administrator@mydomain.com";
    mail.parameters.template = companyhome.childByNamePath("Data
    Dictionary/EMail Templates/userdetailsmail.ftl");
    mail.parameters.text = "Welcome to Alfresco!";
    mail.execute(usr);
    var mailtemplate = companyhome.childByNamePath("Data
    Dictionary/EMail Templates/userdetailsmail.ftl");
    var result = usr.processTemplate(mailtemplate);
    model.result = result;
    }
    main();

  7. Save this file with the name sendusermail.get.js.
  8. We create an HTML template file with the name sendusermail.get.html.ftl and put a very simple one liner code in this file:
    ${result}
  9. Run the script using the URL http://localhost:8080/alfresco/service/com/infoaxon/sendusermail?uname=admin (or put any other username).
  10. If the mail property is properly configured for this user, a mail will be sent to the e-mail ID; and the mail content will be returned. It should be something like this:
    Hello Administrator,
    Welcome to Alfresco!
    Here are your registration details.
    First Name: Administrator
    Last Name:
    User Name: admin
    Enjoy your Stay!
    Regards,
    the Alfresco Team.

How it works…

We first search for the username passed with the querystring. Once it is retrieved (using the people.getPerson method), we use the person's node reference for further processing.

Then we send the mail after preparing the mail object properly. The mail is sent using the actions provided by Alfresco. There are a number of such actions available to be performed in JavaScript API. One such action can be starting a workflow.

Finally, the Mail Template is processed against the user object, and finally the output is returned as the Web Script response.

There's more...

You have seen that you can configure the authentication mechanism of a Web Script in the descriptor XML file. This can be either of none, admin, user, or guest.

However, each time a user is authenticated, Alfresco opens a new session and generates a token for this session. This ticket can be retrieved by the getTicket method of the session global object.

For example,

var tkt = session.getTicket();

The variable tkt now has the authentication token of this session. You can use this ticket for calling of another Web Script like:

http://localhost:8080/alfresco/service/com/infoaxon/sendusermail?alf_ticket=${tkt}&uname=admin

Thus, Alfresco won't ask for new user credentials if you pass a valid ticket in the alf_ticket querystring. However, if the session is expired, Alfresco will again pop up the user credential dialog.

The importance of this mechanism is often you would want to invoke another Web Script from one script in the client/browser side. There you would normally want to accept the user credentials once and use the session multiple times until the user actually leaves. Thus alf_ticket can help you – this name can pass the authentication token to another Web Script – thus sharing sessions.

Summary

In this article we understood the usage of default web scripts library that come with Alfresco. We took a look at several sample web scripts, for example sending e-mails using templates, searching and displaying documents.


Further resources on this subject:


About the Author :


Snig Bhaumik

Snigdhendu Bhaumik is the Technical Director of InfoAxon Technologies Ltd, based in India. He is also the Director of Open Source Innovation and Heads the Knowledge Management Practice at InfoAxon – India’s first and pioneer Open Source Integration company.

A computer engineer by education and developer at heart, Snigdhendu has 10 years of experience in various technologies such as Alfresco, Liferay, Pentaho, and Microsoft .NET. An open source enthusiast and regular community participant, Snigdhendu was the original contributor of Alfresco Calendar component. He specializes in Knowledge Management and Business Intelligence domains, and is responsible for designing and architecting KM and BI solution offerings.

Books From Packt


Alfresco 3 Records Management
Alfresco 3 Records Management

Alfresco 3 Web Content Management
Alfresco 3 Web Content Management

Alfresco 3 Web Services
Alfresco 3 Web Services

Alfresco 3 Business Solutions
Alfresco 3 Business Solutions

Liferay Portal 6 Enterprise Intranets
Liferay Portal 6 Enterprise Intranets

CMS Made Simple 1.6: Beginner's Guide
CMS Made Simple 1.6: Beginner's Guide

CMS Design Using PHP and jQuery
CMS Design Using PHP and jQuery

CMS Made Simple Development Cookbook
CMS Made Simple Development Cookbook


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software