alfresco Folder Maker: A Web Script that handles POST


The Hello World Web Script handles GET HTTP methods. But what if you want to create data on the server? For that your web script should handle POST.

Here is a simple example that creates new folders in Company Home. It is invoked by making a POST call with a JSON body that looks like:


Optionally, you could add a title or a description to the folder by passing those in as part of the body, like:

  'title':'test title',
  'description':'test description'

The descriptor is called

  <shortname>Folder Maker</shortname>
  <description>Creates folders</description>
  <format default="json"></format>

The optional "family" element is a convenient way to group web scripts in the web script index. The "format" element declares that this web script returns JSON.

The controller is called

var name = title = desc = null;

var name = json.get('name');

try {
  title = json.get('title');
} catch (err) {}

try {
  desc = json.get('description');
} catch (err) {}

var folder = companyhome.createFolder(name);

var needsSave = false;

if (title != null) {['cm:title'] = title;
  needsSave = true;

if (desc != null) {['cm:description'] = desc;
  needsSave = true;

if (needsSave) {;
} = folder.nodeRef.toString(); = name;
model.title = title;
model.description = desc;

Notice that this controller has "json" in its name. This tells Alfresco to expect a JSON payload. Alfresco will automatically parse the JSON and put it in a root variable called "json".

The controller grabs the name, title, and description from the JSON and creates the folder using the root scope variable called "companyhome".

If a title or description is passed in those properties get saved.

The values that were passed in as well as the new folder's node reference get set on the model before handing control over to the view.

The view is named

  <#if title??>
  "title": "${title}",
  <#if description??>
  "description": "${description}",
  "id": "${id}",
  "name": "${name}"

This freemarker just echoes back the values set on the model as JSON. The title and description may not always be present, so the view uses the Freemarker null check built-in in an if statement to avoid returning those if they were not set.

Any HTTP client can be used to test this web script out. Here's what it would look like using curl:

jpotts$ curl -uadmin:admin -H "content-type: application/json" -X POST "http://localhost:8080/alfresco/s/example/folders" -d "{'name':'testfolder','title':'test title', 'description':'test desc'}"
  "title": "test title",
  "description": "test desc"
  "id": "workspace://SpacesStore/cc26a12f-306b-41f1-a859-668f11fc2a54",
  "name": "testfolder"

Note that curl is passing in basic auth credentials. In this case it is using "admin". Because this example creates items in Company Home, you must use a user that has the appropriate permissions to do that.

This web script has no real error checking. If you don't pass in a name or you pass in a name that has already been used you will see an error.