[AAT] Chapter 2: Javascript console
Provably you’d already hear about this Javascript console for Alfresco, but if you’re here maybe you don’t know exactly what is it, which is its purpose or with what kind of functionality it is supposes to help you in your daily work. No worries, we are here to clarify this doubts, let’s play!
About this tutorial
- Public target: Alfresco developers or administrators
- Difficulty level: Beginner
- Goal: In this tutorial we are going to review how to install a Javascript module from scratch
What is JavaScript Console?
This is a time saver item created around 2011 by Florian Maul (fme AG) to execute Javascript code on real time in Alfresco (and make developers & admin life easier). Till that moment, if you wanted to execute a script, it was a little boring: Type your code file, upload on Repository> Data Dictionary> Scripts, then call it using the proper url, then you have a syntax error (or any annoying error) and, again, repeat all that tedious process. Of course, you can deploy it in other different ways, but in the plain Alfresco out of the box you will never be able to write and execute your JS on real time. That’s the purpose of this Javascript console: Everything at first sight. Flexible, agile and very useful. In fact, with this console you can also execute Alfresco Webscripts, but we’ll be focused on javascript.
Let’s practice a bit!
Installation
Steps overview
- Download code
- Compile code and generate amp for Alfresco and amp for Share
- Deploy and install both amps
- Run Alfresco and console overview
- Advices and tips
Environment used for this tutorial
- MacOS – High Sierra version 10.13.5 (17F77)
- Alfresco 5.2 community (release 072017)
- Installed at: /Applications/Alfresco/community/clean/alfresco-5.2-072017/ – From now ALF_PATH
- Alfresco amp: ALF_PATH/amps
- Share amps: ALF_PATH/amps_share
- Git installed on local machine: git version 2.15.1 (Apple Git-101)
Let’s play!
- Open a terminal and go to your git hub workspace:
cd git
- Clone and download the project in your workspace:
Cristina$ sudo git clone https://github.com/share-extras/js-console.git Password: Cloning into 'js-console'... remote: Counting objects: 2494, done. remote: Total 2494 (delta 0), reused 0 (delta 0), pack-reused 2494 Receiving objects: 100% (2494/2494), 11.17 MiB | 3.08 MiB/s, done. Resolving deltas: 100% (791/791), done.
Note: Password is the one for your system user, in my case for Cristina user.
- Give permissions to your user:
sudo chown -R Cristina js-console/
cd js-console
- Compile and generate amps:
Cristina$ mvn clean install
Out put:[INFO] Scanning for projects... [INFO] Inspecting build with total of 3 modules... [INFO] Installing Nexus Staging features: [INFO] ... total of 3 executions of maven-deploy-plugin replaced with nexus-staging-maven-plugin [INFO] ------------------------------------------------------------------------ [INFO] Reactor Build Order: [INFO] [INFO] JavaScript Console [INFO] JavaScript Console - Repository [INFO] JavaScript Console - Share [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building JavaScript Console 0.7-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ javascript-console --- [INFO] [INFO] --- maven-enforcer-plugin:1.4.1:enforce (enforce-sdk-requirements) @ javascript-console --- [INFO] [INFO] --- maven-source-plugin:2.4:jar-no-fork (attach-sources) @ javascript-console --- [INFO] [INFO] --- maven-install-plugin:2.5.2:install (default-install) @ javascript-console --- [INFO] Installing /Users/Cristina/git/js-console/pom.xml to /Users/Cristina/.m2/repository/de/fmaul/javascript-console/0.7-SNAPSHOT/javascript-console-0.7-SNAPSHOT.pom [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building JavaScript Console - Repository 0.7-SNAPSHOT [INFO] ... ... ... ... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building JavaScript Console - Share 0.7-SNAPSHOT [INFO] ------------------------------------------------------------------------ ... ... ... [INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary: [INFO] [INFO] JavaScript Console ................................. SUCCESS [ 2.630 s] [INFO] JavaScript Console - Repository .................... SUCCESS [ 11.752 s] [INFO] JavaScript Console - Share ......................... SUCCESS [ 16.807 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 38.926 s [INFO] Finished at: 2018-06-18T11:09:28+02:00 [INFO] Final Memory: 63M/487M [INFO] ------------------------------------------------------------------------
- Amps are located:
... [INFO] Building amp: /Users/Cristina/git/js-console/javascript-console-repo/target/javascript-console-repo-0.7-SNAPSHOT.amp ... [INFO] Building amp: /Users/Cristina/git/js-console/javascript-console-share/target/javascript-console-share-0.7-SNAPSHOT.amp ...
Finally:
- Alfresco repo amp, for alfresco.war: /Users/Cristina/git/js-console/javascript-console-repo/target/javascript-console-repo-0.7-SNAPSHOT.amp
- Share ui amp, for share.war: /Users/Cristina/git/js-console/javascript-console-share/target/javascript-console-share-0.7-SNAPSHOT.amp
- Copy repo amp in the proper place
cp /Users/Cristina/git/js-console/javascript-console-repo/target/javascript-console-repo-0.7-SNAPSHOT.amp /Applications/Alfresco/community/clean/alfresco-5.2-072017/amps
- Copy share amp in the proper place
cp /Users/Cristina/git/js-console/javascript-console-share/target/javascript-console-share-0.7-SNAPSHOT.amp /Applications/Alfresco/community/clean/alfresco-5.2-072017/amps_share/
- Install both amps with apply_amps.sh script. Remember: The ones you have under amps/ will be installed on alfresco.war and amps_share/ under share.war
Cristina$ pwd /Applications/Alfresco/community/clean/alfresco-5.2-072017 Cristina$ cd bin/ Cristina$ ./apply_amps.sh Found java executable in /Applications/Alfresco/community/clean/alfresco-5.2-072017/java This script will apply all the AMPs in amps and amps_share to the alfresco.war and share.war files in /Applications/Alfresco/community/clean/alfresco-5.2-072017/tomcat/webapps Press control-c to stop this script . . . Press any other key to continue . . . [PRESS ENTER] Module 'org.alfresco.integrations.google.docs' installed in '/Applications/Alfresco/community/clean/alfresco-5.2-072017/tomcat/webapps/alfresco.war' - Title: Alfresco / Google Docs Integration - Version: 3.0.4 - Install Date: Wed Jun 20 10:47:34 CEST 2018 - Description: The Repository side artifacts of the Alfresco / Google Docs Integration. Module 'de.fme.alfresco.JavascriptConsole-repo' installed in '/Applications/Alfresco/community/clean/alfresco-5.2-072017/tomcat/webapps/alfresco.war' - Title: fme Javascript Console Repository Extension - Version: 0.7.1806201037 - Install Date: Wed Jun 20 10:47:33 CEST 2018 - Description: Adminstration console module to execute arbitrary javascript code. Module 'alfresco-share-services' installed in '/Applications/Alfresco/community/clean/alfresco-5.2-072017/tomcat/webapps/alfresco.war' - Title: Alfresco Share Services AMP - Version: 5.2.0 - Install Date: Wed Jun 20 10:47:32 CEST 2018 - Description: Module to be applied to alfresco.war, containing APIs for Alfresco Share Module 'alfresco-aos-module' installed in '/Applications/Alfresco/community/clean/alfresco-5.2-072017/tomcat/webapps/alfresco.war' - Title: Alfresco Office Services Module - Version: 1.1.6 - Install Date: Wed Jun 20 10:47:31 CEST 2018 - Description: Allows applications that can talk to a SharePoint server to talk to your Alfresco installation Module 'de.fme.alfresco.JavascriptConsole-share' installed in '/Applications/Alfresco/community/clean/alfresco-5.2-072017/tomcat/webapps/share.war' - Title: fme Javascript Console Share Extension - Version: 0.7.1806201037 - Install Date: Wed Jun 20 10:47:38 CEST 2018 - Description: Adminstration console module to execute arbitrary javascript code. Module 'org.alfresco.integrations.share.google.docs' installed in '/Applications/Alfresco/community/clean/alfresco-5.2-072017/tomcat/webapps/share.war' - Title: Alfresco / Google Docs Share Module - Version: 3.0.4 - Install Date: Wed Jun 20 10:47:36 CEST 2018 - Description: The Share side artifacts of the Alfresco / Google Docs Integration. About to clean out /Applications/Alfresco/community/clean/alfresco-5.2-072017/tomcat/webapps/alfresco and share directories and temporary files... Press control-c to stop this script . . . Press any other key to continue . . . Cleaning temporary Alfresco files from Tomcat...
- Check and verify those amps where installed properly.
Alfresco Repository (alfresco.war):Cristina$ java -jar alfresco-mmt.jar list ../tomcat/webapps/alfresco.war Module 'org.alfresco.integrations.google.docs' installed in '../tomcat/webapps/alfresco.war' - Title: Alfresco / Google Docs Integration - Version: 3.0.4 - Install Date: Wed Jun 20 10:47:34 CEST 2018 - Description: The Repository side artifacts of the Alfresco / Google Docs Integration. Module 'de.fme.alfresco.JavascriptConsole-repo' installed in '../tomcat/webapps/alfresco.war' - Title: fme Javascript Console Repository Extension - Version: 0.7.1806201037 - Install Date: Wed Jun 20 10:47:33 CEST 2018 - Description: Adminstration console module to execute arbitrary javascript code. Module 'alfresco-share-services' installed in '../tomcat/webapps/alfresco.war' - Title: Alfresco Share Services AMP - Version: 5.2.0 - Install Date: Wed Jun 20 10:47:32 CEST 2018 - Description: Module to be applied to alfresco.war, containing APIs for Alfresco Share Module 'alfresco-aos-module' installed in '../tomcat/webapps/alfresco.war' - Title: Alfresco Office Services Module - Version: 1.1.6 - Install Date: Wed Jun 20 10:47:31 CEST 2018 - Description: Allows applications that can talk to a SharePoint server to talk to your Alfresco installation
Share UI (share.war):
Cristina$ java -jar alfresco-mmt.jar list ../tomcat/webapps/share.war Module 'de.fme.alfresco.JavascriptConsole-share' installed in '../tomcat/webapps/share.war' - Title: fme Javascript Console Share Extension - Version: 0.7.1806201037 - Install Date: Wed Jun 20 10:47:38 CEST 2018 - Description: Adminstration console module to execute arbitrary javascript code. Module 'org.alfresco.integrations.share.google.docs' installed in '../tomcat/webapps/share.war' - Title: Alfresco / Google Docs Share Module - Version: 3.0.4 - Install Date: Wed Jun 20 10:47:36 CEST 2018 - Description: The Share side artifacts of the Alfresco / Google Docs Integration.
Everything looks right!
- Start your Alfresco
Cristina$ cd .. Cristina$ ./alfresco.sh start waiting for server to start.... done server started /Applications/Alfresco/community/clean/alfresco-5.2-072017/postgresql/scripts/ctl.sh : postgresql started at port 5432 Using CATALINA_BASE: /Applications/Alfresco/community/clean/alfresco-5.2-072017/tomcat Using CATALINA_HOME: /Applications/Alfresco/community/clean/alfresco-5.2-072017/tomcat Using CATALINA_TMPDIR: /Applications/Alfresco/community/clean/alfresco-5.2-072017/tomcat/temp Using JRE_HOME: /Applications/Alfresco/community/clean/alfresco-5.2-072017/java Using CLASSPATH: /Applications/Alfresco/community/clean/alfresco-5.2-072017/tomcat/bin/bootstrap.jar:/Applications/Alfresco/community/clean/alfresco-5.2-072017/tomcat/bin/tomcat-juli.jar Using CATALINA_PID: /Applications/Alfresco/community/clean/alfresco-5.2-072017/tomcat/temp/catalina.pid Tomcat started. /Applications/Alfresco/community/clean/alfresco-5.2-072017/tomcat/scripts/ctl.sh : tomcat started
- Check catalina.out and verify our amps are properly started
tail -n500 -f tomcat/logs/catalina.out
Output:... INFO [repo.module.ModuleServiceImpl] [localhost-startStop-1] Starting module 'de.fme.alfresco.JavascriptConsole-repo' version 0.7.1806201037. ... INFO [config.packaging.ModulePackageManager] [localhost-startStop-1] fme Javascript Console Share Extension, 0.7.1806201037, Adminstration console module to execute arbitrary javascript code. ....
- Access into Share, login as admin user. Click on
Admin Tool > Java Script Console
- Let’s try the default example, click on right side, button “Load Script”, select “Default Demo Script” and execute it:
- Below we can check in “output” tab, the results:
- Let’s try another example, click on “Load Script”, select “alfrescodocs.js.sample” and click execute
- There’s no output because this script will create a document txt under “Repository” with list of results which contains “alfresco” term
- Click on Repository and check alf docs.txt content:
Congrats! You have installed Alfresco Javascript Console and launch 2 scripts to learn how is working.
Next steps.
As you know already, with this console we can execute script code, edit and check it’s result on real time. Therefore, as administrator you can save time making your own scripts to extract information, reports and so. By the way, there is a really big list of examples you can reuse, just check here for some useful links:
- JavaScript API Cookbook
- Alfresco JavaScript API
- Some useful Javascript Console scripts
- And much more!
We hope this article will help in your daily work. If you have any suggestion, please ping us on twitter 🙂 See you in next chapter!