- In your IDE, navigate to the repo-amp/src/main/amp/config/alfresco/extension/templates/webscripts folder.
In the next part of this tutorial, you create three files for the test web script, modeltest.get.desc.xml, modeltest.get.html.ftl, and modeltest.get.js in the webscripts folder.
-
In the webscripts folder, create the web script
description file, modeltest.get.desc.xml, with the
following content:
<webscript> <shortname>Model test</shortname> <family>Tutorials</family> <description>Creates content of a custom type</description> <url>/modeltest/{documentName}</url> <format default="html">extension</format> <authentication>user</authentication> </webscript>
You can specify the name of the document to create - it is passed as a parameter via the URL. To keep things simple the type of the content is hardcoded in the JavaScript code to be of type my:whitepaper.
-
In the webscripts folder, create a new JavaScript file,
modeltest.get.js, with the following content:
var contentType = "my:whitepaper"; var documentName = url.templateArgs.documentName; var document = companyhome.createNode(documentName, contentType); if (document != null){ model.document = document; model.msg = "Created OK!"; } else { model.msg = "Failed to create document!"; }
The web script simply creates a new document of type whitepaper. If the operation fails an error message is recorded and this will be displayed by the corresponding template file. If the operation is successful, the reference to the created document itself is stored in the model for use by the template.
-
In the webscripts folder, create a new FreeMarker template
file, modeltest.get.html.ftl, with the following
contents:
<p>Creating the following document:</p> <ul> <li>${document.name}</li> <li>${document.type}</li> </ul> <b>${msg}</b>
The template file simply extracts the name and type from the document object and displays it, along with the message passed from the JavaScript code.
- Point your web browser at the web scripts index at http://localhost:8080/alfresco/service/index.
- You can click Browse 'Tutorials' Web Scripts to view your web script and confirm that it is indeed present.
-
Run the web script by specifying a URL such as the following (you can change
the name of the document if you wish):
http://localhost:8080/alfresco/service/modeltest/MyWhitepaper
You will see a page displayed such as the following:
Creating the following document: MyWhitepaper {http://www.mycompany.com/model/content/1.0}whitepaper Created OK!
Note that the fully qualified type name is as specified in your custom model file. If you try to create a content of a type that does not exist you will get an error.
- Run the web script a few times to create several documents.
- Log into Share. You can use the Node Browser to search for nodes of the custom type.
-
On the main Share menu select Admin Tools and then from
the left-hand Tools Menu select Node
Browser.
The Node Browser interface will be displayed.
-
In the search box type the following query:
TYPE:"{http://www.mycompany.com/model/content/1.0}whitepaper"
-
Click the Search button.
A list of nodes with the specified type, my:whitepaper, will be returned.