Home > Alfresco > 5 Easy Steps To Get All Root Groups in Alfresco Using Web Scripts

5 Easy Steps To Get All Root Groups in Alfresco Using Web Scripts

Get All Root Groups in Alfresco

I was too curious and wonder how I can get or visibly see all the groups in Alfresco aside from using the Admin Console functionality.  I found out that it is possible using web scripts.

The Problem

I want to display in a web browser the list of all groups in Alfresco using a simple java-script web script.

The Solution

NOTE: I will assume you know how to create a web script, or at least have an idea. =P

1. Log in to your Alfresco as admin.

2. Locate ‘Web Scripts Extensions‘ folder (Company Home > Data Dictionary > Web Scripts Extensions)

Figure 1: Web Scripts Extensions Page

This folder is where we usually put our custom web scripts. By default, custom web scripts should be pit in this folder.

3. Create a folder (any name will do, e.g. getAllGroups) inside the Web Scripts Extensions folder.

4. Under the folder created in step 3, we create the 3 files below:

a. getAllGroups.get.desc.xml – the descriptor file that is responsible for registering our web script in Alfresco. This file will containt the following code

<webscript>
    <shortname>Gets All the Groups  in Alfresco</shortname>
    <description>This  is a sample web script</description>
    <url>/get/all/groups</url>
    <format default=”html”/>
    <authentication>admin</authentication>
    <transaction>required</transaction>
</webscript>

The explanation for each tags can be found here.

b. getAllGroups.get.html.ftl – free-marker template, used to represent the response data from Alfresco. Below is an example with formatting.

<html>
   <body>
       <table>
       <thead>
           <tr>
               <th>Short Name</th>
               <th>Full Name</th>
               <th>Display Name</th>
           </tr>
       </thead>
       <#list alfrescoGroups as g>
           <tr>
               <th>${g.getShortName()}</th>
               <th>${g.getFullName()}</th>
               <th>${g.getDisplayName()}</th>
           </tr>
       </#list>
       <table>
   </body>
</html>

c. getAllGroups.get.js – this is where the logic of the code is located. Alfresco exposed some javascript  API’s which developers can use to access Alfresco repository. For groups and authority related API, we will use the Groups (Authority Service). Below is a sample code using groups API:

/*@author – anythingalfresco: Gets all the root groups in Alfresco*/

//javascript API that gets all the root groups in Alfresco
var myGroups = groups.getAllRootGroups();

model.alfrescoGroups = myGroups;

5. After creating these 3 files, go to the web scripts index page at http://localhost:8080/alfresco/service/index and click Refresh Web Scripts button. Invoke the web script uri (http://localhost:8080/alfresco/service/get/all/groups) and the result should be like the one below:

Figure 2: Sample Result

Now we are done! It’s quite easy? You can try the code in your Alfresco installation and see if it works. BTW, I’ve used Alfresco Enterprise 3.2r in this example. But, it should work in higher versions too. If you want to know more about about Alfresco web scripts, you can visit the Alfresco wiki. =P

Advertisements
  1. March 10, 2013 at 9:47 am

    I agree 100%

    easy web scripts
    http://easywebscripts.com

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: