cancel
Showing results for 
Search instead for 
Did you mean: 

Custom action using server side javascript

robert_begin
Champ on-the-rise
Champ on-the-rise
I would like to add a new action on Share's document-browse page (Alfresco 4.1) which executes a server side-javascript residing in repo at companyhome\Data Dictionary\scripts\mypublish.js

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.
10 REPLIES 10

sujaypillai
Confirmed Champ
Confirmed Champ
Hello Robert,

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!!!

robert_begin
Champ on-the-rise
Champ on-the-rise
I realize that java is the answer for everything.  But all I want to do is run a simple server-side javascript when an icon is clicked on the document-browse Share page.  So the question is how do I configure Spring in order to tie the icon to run the javascript ?

Ok … here it goes!!!

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 -

I can't believe that all I was missing was these 2 paramaters:

            <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

How should we add the icon image in the above code?

kaynezhang
World-Class Innovator
World-Class Innovator
Implement  action using sever-side javascript saved in repository is complicated than saved in classpath.I'm afraid there is no directy way through spring configuration.
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.

robert_begin
Champ on-the-rise
Champ on-the-rise
Thanks! I'm sticking with the classpath solution.  One more question:  How do I reference a parameter from the onactionFormDialog withing the server side javascript ?

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!

marcello_modica
Champ in-the-making
Champ in-the-making
I need to get parameteres inserted in onActionFormDialog, inside the simple javascript referenced in script bean. How i do it?

dab
Champ in-the-making
Champ in-the-making
Marcello and Robert:
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.