Custom action using server side javascript

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2013 03:58 PM
The javascript transforms the doc to PDF and moves it to a static folder. (No client-side javascript is required)
<!–break–>
The javascript works fine when tied to a folder rule, but I want the users to choose my "Publish to PDF" icon when a particular document needs to be published (not all docs in particular folder need to be published)
I've added this to share-config-custom.xml :
<!– Custom DocLibActions config section –> <config evaluator="string-compare" condition="DocLibActions"> <actions> <action id="mypublish" icon="publish" type="javascript" label="actions.mycompany.mypublish"> <param name="function">onActionSimpleRepoAction</param> <param name="action">mypublish</param> </action> </actions> <!– Action Group definitions –> <actionGroups> <actionGroup id="document-browse"> <action index="400" id="mypublish" /> </actionGroup> </actionGroups> </config>
So the icon appears on the document-browse Share page, but clicking the icon does not launch the s-s javascript.
BUT how do I configure this through Spring ???
Noted from JPotts:
A quick side-note before moving on to the front-end: The action executer examples shown in Part 1
were both implemented in Java. However, action executers can also be implemented using server-side
JavaScript. The steps are the same: write the logic, then configure it through Spring.
I know I can accomplish this with the "document-execute-script" action, but the user has the extra step of selecting the proper javascript.
- Labels:
-
Archive
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2013 01:33 AM
The best way to understand this would be going through "Transform" action available by default in Alfresco. You can get the code for this action in -HEAD\root\projects\repository\source\java\org\alfresco\repo\action\executer\TransformActionExecuter.java
And the Spring configuration for this goes in - tomcat\webapps\alfresco\WEB-INF\classes\alfresco\action-services-context.xml
<bean id="transform" class="org.alfresco.repo.action.executer.TransformActionExecuter" parent="action-executer"> <property name="dictionaryService" ref="dictionaryService" /> <property name="nodeService" ref="NodeService" /> <property name="checkOutCheckInService" ref="CheckOutCheckInService" /> <property name="contentService" ref="ContentService" /> <property name="copyService" ref="CopyService" /> <property name="mimetypeService" ref="mimetypeService" /> <property name="applicableTypes"> <list> <value>{http://www.alfresco.org/model/content/1.0}content</value> </list> </property> </bean>
Hope so that helps you!!!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2013 08:27 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2013 12:55 AM
Step 1: Create a .js file (transformToPdf.js) with the below code
document.transformDocument("application/pdf");
Place this file under location : <b>tomcat\shared\classes\alfresco\extension\scripts</b>
Step 2: Create a context file for spring configuration (custom-action-services-context.xml) under tomcat\shared\classes\alfresco\extension with the below code
<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'><beans> <bean id="transform-to-pdf-action" parent="script"> <property name="scriptLocation"> <bean class="org.alfresco.repo.jscript.ClasspathScriptLocation"> <constructor-arg> <value>alfresco/extension/scripts/transformToPdf.js</value> </constructor-arg> </bean> </property> </bean></beans>
Step 3: Adding the link on Share side to call the action. Add the below code snippet to tomcat\shared\classes\alfresco\web-extension\share-config-custom.xml
<config evaluator="string-compare" condition="DocLibActions"> <!– Action definitions –> <actions> <!– Transfor Document To PDF –> <action id="transform-to-pdf" type="javascript" label="Tranform To PDF"> <param name="function">onActionSimpleRepoAction</param> <!– Additional parameters for onRepoAction function –> <param name="action">transform-to-pdf-action</param> <param name="successMessage">Successfully transformed to PDF</param> <param name="failureMessage">PDF Conversion failed</param> </action> </actions> <actionGroups> <actionGroup id="document-browse"> <action index="460" id="transform-to-pdf"/> </actionGroup> <actionGroup id="document-details"> <action index="400" id="transform-to-pdf"/> </actionGroup> </actionGroups> </config>
Note : The above code creates a .pdf file in the same location from which it is acted upon. So for your requirement you would be changing the code in transformToPdf.js accordingly.
Attached is the screenshot how it looks on Share side -

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2013 09:10 AM
<param name="successMessage">Successfully transformed to PDF</param> <param name="failureMessage">PDF Conversion failed</param>
Do you know how to specify that the js script resides in the repo under DataDictionary\Scripts\ instead of file system ?
Robert
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2017 02:05 PM
How should we add the icon image in the above code?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2013 11:41 AM
If you really want to place javascript in repository you can try following steps
1.In share-config-custom.xml configure your action transform-to-pdf using "script" action instead of your "transform-to-pdf-action"
<action id="transform-to-pdf" type="javascript" label="Tranform To PDF"> <param name="function">onActionSimpleRepoAction</param> <!– Additional parameters for onRepoAction function –> <param name="action">transform-to-pdf-action</param> <param name="successMessage">Successfully transformed to PDF</param> <param name="failureMessage">PDF Conversion failed</param> </action>
2.place your transformToPdf.js in
Company Home/Data Dictionary/Scripts/transformToPdf.js
3.add your script path parameter to transform-to-pdf action
<action id="transform-to-pdf" type="javascript" label="Tranform To PDF"> <param name="function">onActionSimpleRepoAction</param> <!– Additional parameters for onRepoAction function –> <param name="action">transform-to-pdf-action</param> <param name="script-ref">Company Home/Data Dictionary/Scripts/transformToPdf.js</param> <param name="successMessage">Successfully transformed to PDF</param> <param name="failureMessage">PDF Conversion failed</param> </action>
This solution is not that elegant and I have not tested it yet.I suggest you use the classpath one.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2013 08:27 AM
Here's my "share-custom-config.xml"
<!– Custom DocLibActions config section –> <config evaluator="string-compare" condition="DocLibActions"> <actions> <action id="my-document-new-publish" icon="publish" type="javascript" label="actions.mycompany.mynewpublish"> <param name="function">onActionFormDialog</param> <param name="itemKind">action</param> <param name="itemId">my-newpublish-action</param> <param name="mode">create</param> <param name="destination">{node.nodeRef}</param> <param name="successMessage">message.mycompany.mynewpublish.success</param> <param name="failureMessage">message.mycompany.mynewpublish.failure</param> </action> </actions> <actionGroups> <actionGroup id="document-browse"> <action index="366" id="my-document-new-publish" /> </actionGroup> </actionGroups></config><config evaluator="string-compare" condition="my-newpublish-action"> <forms> <form> <field-visibility> <show id="myTarget"/> </field-visibility> <appearance> <field id="myTarget" label="Choose destination"> <control template="/org/alfresco/components/form/controls/selectone.ftl"> <control-param name="options">0|Intranet,1|Public Site</control-param> </control> </field> </appearance> </form> </forms></config>
My "shared\classes\alfresco\extension\mynewpublish-context.xml"
?xml version='1.0' encoding='UTF-8'?><!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'><beans><bean id="my-newpublish-action" parent="script"> <property name="scriptLocation"> <bean class="org.alfresco.repo.jscript.ClasspathScriptLocation"> <constructor-arg> <value>alfresco/extension/scripts/mynewpublish.js</value> </constructor-arg> </bean> </property></bean></beans>
So how do I reference the myTarget parameter from within my server-side javascript mynewpublish.js in order to get the expected 0 or 1 value ?
var ret = action.parameters["myTarget"];
does not work. Hard to find examples anywhere.
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2014 01:37 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2014 05:18 PM
The issue at https://issues.alfresco.com/jira/browse/ALF-15969 seems to suggest that it is not possible to accomplish what you and I wish. I suggest that you obtain a JIRA account and vote for the issue as I have done.
