cancel
Showing results for 
Search instead for 
Did you mean: 

[SOLVED]How to Enable/Disable an action following conditions

nicolasc92
Champ in-the-making
Champ in-the-making
Hello to everybody,

In following the Wiki page http://wiki.alfresco.com/wiki/Custom_Document_Library_Action, I have created a new action in Share (v3.2) for documents and it works well, I mean I have added the new action in <actionSet id="document"> only (in document.list.get.config.xml).

However I would like to disable/hide the action if the document isn't in a certain extension.
For the meantime I have set an alert in the javascript function (in documentlist-min.js) but I don't wish to display the action all the time.

If anybody knows how to make a filter (to display/hide an action) and which files are concerned thank you for you help .
6 REPLIES 6

mikeh
Star Contributor
Star Contributor
You can use the permissions attribute to selectively hide actions depending on any arbitrary logic.

Take a look at doclist.get.js and doclist.get.json.ftl (/alfresco/service/script/org/alfresco/slingshot/documentlibrary/doclist.get) on the Repository (alfresco.war). Add the logic that determines whether your document should have the action or not within the loop
for each (asset in assets)
{
   …
}
and add the result to the items object, e.g. enableMyAction: true/false

Then in the userAccess section (the one inside the loop, not the one near the beginning of the file) of doclist.get.json.ftl add your new pseudo-permission:
"my-action": ${item.enableMyAction?string}

Finally in the documentlist.get.config.xml file, add the permission to your new custom action:
<action type="action-link" id="onActionMyAction" permission="my-action" label="actions.document.my-action" />

The action will be hidden unless the user has the "my-action" pseudo-permission that you calculated on the Repository webscript.

Thanks,
Mike

nicolasc92
Champ in-the-making
Champ in-the-making
Thank you Mike for your precise answer !   Smiley Very Happy
I am going to test this and I keep you informed.

nicolasc92
Champ in-the-making
Champ in-the-making
Hello,

There is one thing I don't understand.
When I modify doclist.get.js it doesn't have effect on the execution and I can even delete  it :? 
It seems that this file isn't included and with firebug extension I don't find it in the script list.
Otherwise how can I add a permission function like hasPermission .
May I add one personnal function, for example  isAPdfFile(asset, obj) which returns true/false and call this function in doclist.get.js or elsewhere ?

Thank you in advance for your help.

mikeh
Star Contributor
Star Contributor
doclist.get.js is a Repository-based server script, so it won't show up in Firebug. You need to reset webscripts in order for changes to take effect. Go to http://localhost:8080/alfresco/service/index (replace with your server/port as required).

No reason why you can't wrap your code in a function.

Thanks,
Mike

nicolasc92
Champ in-the-making
Champ in-the-making
Thanks for everything Mike !
Now I know how to add a custom permission for an action and the thing I wanted to do works.

ggol
Champ in-the-making
Champ in-the-making
Hi to everybody,

I had a similar project, but for Alfresco 3.3.
The action button should only enabled, if a folder has a specific string/name: (eg.:  order_1234)
In version 3.3. the files mentioned by Mike were modified:
So here is what i did:
in doclist.get.js there is now this loop:

for each (node in nodes) {


// my modification: (only a simplified example)
     if (node.name.substr(0,6) == "order_") {
           item.actionPermissions["my-action"]=true;
      }
      else {
       item.actionPermissions["my-action"]=false;
      }

//end my modification

items.push(item);
}

In doclist.get.json.ftl the section mentioned above by Mike moved to item.lib.ftl - and here is nothing to do: because the new action permission was added as  item.actionPermissions["my-action"] wich is processed by the loop as shown:


"userAccess":
      {
     <#list item.actionPermissions?keys as actionPerm>
         <#if item.actionPermissions[actionPerm]?is_boolean>
         "${actionPerm?string}": ${item.actionPermissions[actionPerm]?string}<#if actionPerm_has_next>,</#if>
         </#if>
      </#list>
    
      }


So, if one changed the documentlist.get.config.xml and one reset the webscripts and finally restarted tomcat, it should work.
Thanks to Mike for his post!

Greg