Using the Javascript Console: Creating and populating datalists

This is the first post in a series of posts to showcase the abilities of the Alfresco Javascript Console. The Javascript Console is an Alfresco admin console extension to help developers and administrators to easily develop and execute Javascript against the Alfresco repository. For more information on the Javascript Console and how to install it, see the share-extras wiki page.

In this first installment I’d like to show how to create datalists and datalist items from Alfresco Repository Javascript which can be useful when you want to populate a site’s datalists automatically.

Using the space context to select the site

The first thing you should do in the Javascript Console is to select the space/folder the script is executed in. This automatically puts the node you have selected in a variable called “space”. In this example script we are selecting the Share site where the datalist shall be created. In my local installation I chose the site “jstest” as you can see in the screenshot.To make it even clearer which space my script is expecting I start by assigning it to a variable called “site”:

var site = space;

Now we have a variable site available pointing to the site where the datalist shall be created.

Preparing the site

The “datalists” container within a site may not exist. We need to make sure that it is available or create it if it is missing. Share usually creates this folder automatically when you create your first datalist but if you don’t have a list yet, it might be missing.

var dataLists = site.childByNamePath("dataLists");

if (!dataLists) {
  var dataLists = site.createNode("dataLists", "cm:folder");

  var dataListProps = new Array(1);
  dataListProps["st:componentId"] = "dataLists";
  dataLists.addAspect("st:siteContainer", dataListProps);;

  logger.log("Created new datalists folder.");'

Creating the datalist

Now let’s create a datalist. I chose the existing contacts datalist type for this example. You can have a look in the datalists model to see which types are available and which properties can be set on each type.  Usually datalists are created with cryptic UUID names (i.e. they don’t have names defined for them) but if you create them yourself you can give them any name you like so you are able to find them later on. The Share UI only displays the datalists title, the name doesn’t show up in Share. I am using “contactlist1” as the name for the new datalist:

var contactList = dataLists.childByNamePath("contactlist1");

if (!contactList) {
  var contactList = dataLists.createNode("contactlist1","dl:dataList");

  // tells Share which type of items to create["dl:dataListItemType"] = "dl:contact";;

  var contactListProps = [];
  contactListProps["cm:title"] = "My Contacts";
  contactListProps["cm:description"] = "A contact list generated by a javascript.";
  contactList.addAspect("cm:titled", contactListProps);

  logger.log("Created contact datalist.");

Adding items

Now that we have created a datalist we are ready to place items in the datalist. Since we created a contacts list we need to place items of the type dl:contact in it. We basically create a new node, set the properties of the contact node and save it, that’s all.

var contact = contactList.createNode(null, "dl:contact")["dl:contactFirstName"] = "Florian";["dl:contactLastName"] = "Maul";["dl:contactEmail"] = "";["dl:contactCompany"] = "fme AG";["dl:contactJobTitle"] = "Senior Consultant";["dl:contactPhoneMobile"] = "not available";["dl:contactPhoneOffice"] = "not available";["dl:contactNotes"] = "Alfresco Expert";;
logger.log("Created new contact: " + contact.nodeRef);

When you run the complete script you see that the contact list and one contact items are created. Naturally you can create multiple contact items of you like.

One use case for this technique could be to create a lot of items for performance testing during development or you could generate a Javascript file based on this template to migrate content to the datalists.

Wrapping up

Of course this script doesn’t need to run in the Javascript Console. You could also execute it using the Execte Script Action, my Scripts Bootstrap extension or a custom webscript. You just have to make sure you have the node of the site to start with.

Note that the logger.log() messages are printed to the output area. In the Javascript Console these are always displayed regardless of the log level of the ScriptLogger which normally controls the logger output to the Alfresco log file.

I have several other javascript snippets to share with you, so stay tuned for more posts from me on “Using the Javascript Console”. Feedback, questions or ideas for other Alfresco scripting use cases are always welcome.


10 thoughts on “Using the Javascript Console: Creating and populating datalists

  1. Definitively the Javascript console is a fantastic tool.
    Just a comment about how you created the datalist folder, I think it would be safer if you would use the site service to do it instead of just assuming that it is a standard folder, it would also be easier.
    Maybe something like this:

    var site = sites.getSite(;
    var dataLists = site.getContainer(“datalist”);

  2. Pingback: La consola Javascript de Alfresco Share - Blog -

  3. Hi, I would like to know how to insert dates
    this fails:
    //error["bak:bak_fec"] = “2010-10-10″;["bak:bak_fec"] = “2010/10/10″;

    //in white
    var fecha= new Date(2010, 10, 10);
    var ISODate = utils.toISO8601(fecha);["bak:bak_fec"] = ISODate;

    thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>