Alfresco Share Dashlet development tips and hints
Dave Gafford, UI Developer 16 Apr 2009
Through this post I hope to provide a useful insight into key aspects of creating a custom dashlet for Alfresco’s Share web application, using the Surf framework and communicating with Alfresco’s content repository.
Dashlets can be created without Alfresco content repository access e.g. pulling in a feed from an external RSS feed like BBC News. However the Todo-list Ixxus created communicates between the Share UI, Surf webscripts and Alfresco webscripts over HTTP, with the list items being stored as content nodes in the Alfresco repository.
Setting up the dashlet component
Just like any other alfresco webscript, we need to start with the xml description file.
Create folder in the following location (relative to your setup) AlfrescoRoot/webapps/share/custom/WEB-INF/classes/alfresco/site- webscripts/org/alfresco/components/dashlets and add the following xml file called todolist.get.desc.xml.
<webscript> <shortname>To-Do List</shortname> <description>To-Do List</description> <family>user-dashlet</family> <url>/todo/todolist</url> </webscript>
More details on creating simple dashlets can be found in the slides accompanying the Optaros Surf Code Camp.
Along side the desc file we need to have the freemarker template to render the html of the dashlet itself (todolist.get.html.ftl), the javascript backing the template (todolist.get.js) and an optional “head” ftl that allows any CSS and JS files specific to the dashlet to be added to the <head> of the share homepage.
For our todo-list we added a custom CSS file and JS file along with CSS and JS for the YUI calendar component. Our todolist.get.head.ftl looks like this:
<!-- Individual YUI CSS files -->
<link rel="stylesheet" type="text/css" href="${page.url.context}/yui/calendar/assets/skins/default/calendar.css">
<!-- Individual YUI JS files -->
<script type="text/javascript" src="${page.url.context}/yui/calendar/calendar.js"></script>
<!-- Todo List Assets -->
<link rel="stylesheet" type="text/css" href="${page.url.context}/components/dashlets/todo-list.css"/>
<script type="text/javascript" src="${page.url.context}/components/dashlets/todo-list.js"></script>You could also have inline CSS and JS code in this template if needed.
Refresh your webscripts by going to http://yourhost:8080/share/service/index and clicking the Refresh Web Scripts button.
Then go to the Share dashboard and click “Customize Dashboard”, your new dashlet should appear in the list after clicking the “Add Dashlets” button.
Retrieving remote JSON data from Alfresco
For our todo-list dashlet we need to make a few remote calls to alfresco to retrieve data.
For this we use the remote variable and connectors in the JS webscript backing our template.
var connector = remote.connect("alfresco");
var data = connector.get("/todo");On the Alfresco side we need a webscript with a corresponding url “/todo” that can return the data we need. In our case we have a webscript that returns us a JSON string containing an array of tasks and categories.
NOTE: To get JSON from Alfresco to Surf remember to escape the data in the (alfresco app) JSON ftl using the following:
<#escape x as jsonUtils.encodeJSONString(x)>
<#-- JSON inside here e.g. -->
{
“tasks”:
[
{ “description”: “task one…” },
{ “description”: “task two” }
]
}
</#escape>Once we have our response JSON we create a JSON object from the data returned and can then access our “tasks” and “categories” values, e.g.
// create json object from data
var results = eval('(' + data + ')');
// store tasks on model object for use in rendering template
model.tasks = results["tasks"];In this example the “tasks” property will be available as a root variable in the associated freemarker template, in this instance an array, which we can iterate through using the list directive.
<#list tasks as task>
<p>${task.description}</p>
</#list>Sample Dashlet
Ixxus recently released a Todo-list dashlet to the Alfresco Content Community.






Comments
Comment 1
Claudia Saleh said:
I was very happy to find this dashlet, because is something that I thought was missing in Share. I tried to install it and despite the installation notes being a bit different from the structure in the zip file, I was able to actually installed and recognized. I added the Dashlet, but when it was time to create the task, I got a screen with the message:
"Do you want to save this file or find a program online to open it"
Name:editcreate7d4ea39e
type: unknown file type
from:localhost
any idea why is this happening? I guess at this point Share would be talking to the repository to create the nodes right?
Posted: 09:23pm on 8 Jun 2009
Comment 2
Tester said:
i have deplozed your example, but there is a bug :
An error has occured in the Share component: /share/service/todo/todolist.
It responded with a status of 500 - Internal Error.
Error Code Information: 500 - An error inside the HTTP server which prevented it from fulfilling the request.
Error Message: 06210003 Failed to load script '/org/alfresco/components/dashlets/todo/todolist.get.js (in classpath store file:C:/Alfresco3.2/tomcat/webapps/share/WEB-INF/classes/alfresco/site-webscripts)': 06210002 TypeError: Cannot parse XML: Scanner State 24 not Recognized (file:C:/Alfresco3.2/tomcat/webapps/share/WEB-INF/classes/alfresco/site-webscripts/org/alfresco/components/dashlets/todo/todolist.get.js#7(eval)#1)
Server: Alfresco Community v3.2.0 (2039) schema 2 019
Time: 21.07.2009 16:34:32
Click here to view full technical information on the error.
src of todolist.get.js:
// call over to Alfresco and fetch the todo list
var connector = remote.connect("alfresco");
var data = connector.get("/todo");
// create json object from data
var results = eval('(' + data + ')');
model.tasks = results["tasks"];
// do array unique thingy
var catArray = results["categories"];
Array.prototype.unique = function () {
var r = new Array();
o:for(var i = 0, n = this.length; i < n; i++)
{
for(var x = 0, y = r.length; x < y; x++)
{
if(r[x]==this[i].category)
{
continue o;
}
}
r[r.length] = this[i].category;
}
return r;
}
if (catArray) {
var unique = catArray.unique();
model.cats = unique;
} else {
model.cats = [];
}
Posted: 11:39am on 21 Jul 2009
Comment 3
mark schlossberg said:
I was able to successfully deploy the todo list in my local Share environment, but found that the calendar, edit and delete buttons aren't working -- nothing happens.
I am also confused by the "custom" directory in the filepath above and in the directory structure of the zip file; I didn't have a custom directory. So when I installed I skipped this directory and installed directly in share/WEB-INF and share/components.
I then redid the installation, this time adding the 'custom' subdirectories under Alfresco and Share. This time the dashlet did not appear in the config page at all.
Posted: 08:11pm on 25 Jul 2009
Comment 4
sameer said:
Hello Dave,
I am trying for similar customization.
But I am getting object not defined at
remote.connect("alfresco");
Posted: 02:48am on 31 Aug 2009
Comment 5
Paolo said:
Thanks a lot for this very useful example!
I'm developing my own dashlet using the template defined by yours: for me, you example it has been very useful in order to start to develop my own share customizations.
Posted: 01:44pm on 10 Oct 2009
Add your comment