cancel
Showing results for 
Search instead for 
Did you mean: 

Javascript list members of groups

patrickvanhoof
Confirmed Champ
Confirmed Champ

Hi,

I have following code to list members of Alfresco groups:

var siteGroups = new Object(); 
var logFile = space.childByNamePath("GroupAndUsers.txt");

if (logFile == null)
{
logFile = space.createFile("GroupAndUsers.txt");
}

// return all results, skip 0
var paging = utils.createPaging(-1, 0);

siteGroups = groups.getGroups(null, paging);

for (var i=0; i<siteGroups.length; i++)
{
if (siteGroups[i].displayName.indexOf("gl_aa_alfresco_") == -1 &&
siteGroups[i].displayName.indexOf("gl_center_") == -1 &&
siteGroups[i].displayName.indexOf("site_") == -1)
{

logFile.content += siteGroups[i].displayName + "\n";

var group = people.getGroup(siteGroups[i].id);
var members = people.getMembers(group,false);

for (var member in members)
{
logFile.content += member.properties.userName + "\n";
}
}
}

logFile.save();
logFile.content; 12345678910111213141516171819202122232425262728293031323334

But line

  var members = people.getMembers(group,false);

gives an error. What is wrong?

Using Alfresco 5.0.3.

Thanks,

Patrick

1 ACCEPTED ANSWER

jpotts
World-Class Innovator
World-Class Innovator

The error is that the group you are passing is not defined. This is because when you call people.getGroup you are passing in siteGroups[i].id but there is no "id" property of a SiteGroup object.

Instead, try passing in siteGroups[i].fullName, see ScriptGroup object | Alfresco Documentation 

View answer in original post

6 REPLIES 6

patrickvanhoof
Confirmed Champ
Confirmed Champ

Error message is:

2019-01-28 14:09:18,134 ERROR [org.springframework.extensions.webscripts.AbstractRuntime] [http-apr-8080-exec-45] Exception from executeScript - redirecting to status template error: 002815346 Failed to execute script 'workspace://SpacesStore/7eb39344-f918-40e8-8d96-ddaa60be2b09': Group is a mandatory parameter
org.alfresco.scripts.ScriptException: 002815346 Failed to execute script 'workspace://SpacesStore/7eb39344-f918-40e8-8d96-ddaa60be2b09': Group is a mandatory parameter

jpotts
World-Class Innovator
World-Class Innovator

The error is that the group you are passing is not defined. This is because when you call people.getGroup you are passing in siteGroups[i].id but there is no "id" property of a SiteGroup object.

Instead, try passing in siteGroups[i].fullName, see ScriptGroup object | Alfresco Documentation 

Thanks Jeff, that works. But one more question concerning getting the users per group. I have code

		var group = people.getGroup(siteGroups[i].fullName); 
var members = people.getMembers(group,false);

for (var member in members)
{
var memberNode = people.getPerson(member.userName);

if (memberNode != null)
{

logFile.content += " - " + memberNode.properties["lastName"] + ";" + memberNode.properties["firstName"] + ";" + memberNode.properties["email"] + "\n";
}
}

but I do not get any users listed in logFile... so I must do something wrong there too...

Any help much appreciated!

Hello,

With code line  "for (var member in members)"  what you are really doing is an iteration through indexes of the array members .

So to access each individual member object you need to write members[member] inside the loop.

Your code  should be somethig like:

var group = people.getGroup(siteGroups[i].fullName); 
var members = people.getMembers(group,false);

for (var member in members)
{
var memberNode = people.getPerson(members[member].properties["userName"]);
if (memberNode != null)
{
logger.log(" - " + memberNode.properties["lastName"] + ";" + memberNode.properties["firstName"] + ";" + memberNode.properties["email"]);
}
}‍‍‍‍‍‍‍‍‍‍‍‍‍

Regards

It should just be

for (var j=0; j<members.length; j++) 
{
logFile.content += " - " + members[j].properties.lastName + ";" + members[j].properties.firstName + ";" + members[j].properties.email + "\n";
}

Yes, with a little refactoring this is a more accurate solution.

Regards