cancel
Showing results for 
Search instead for 
Did you mean: 

webscript to assign a user to a group

grosisimo
Champ in-the-making
Champ in-the-making
Hi

I was trying to add a user to a group using webscript.
The files created are

AddUserToGroup.get.desc.xml:

<webscript>
  <shortname>AddUserToGroup</shortname>
  <description>AddUserToGroup</description>
  <url>/sample/AddUserToGroup</url>
  <format default="html">argument</format>
  <authentication>admin</authentication>
  <transaction>required</transaction>
</webscript>


AddUserToGroup.get.js:

var user = people.getPerson("ggc");
var group = people.getGroup("Gerentes");

if (user == undefined)
{
   status.code = 404;
   status.message = "User not found";
   status.redirect = true;
}
else if (group == undefined)
{
   status.code = 404;
   status.message = "Group not found";
   status.redirect = true;
}
else
{
   people.addAuthority(group,user);
}

AddUserToGroup.get.html.ftl:

<html>
   <head>
      <title>Groups</title>
   </head>
   <body>
      User assigned to group
   </body>
</html>

The code is very simple (It's just for testing) but it doesn't work.
When i try it, it throws
The Web Script /alfresco/service/sample/AddUserToGroup has responded with a status of 404 - Not Found.

404 Description:   Requested resource is not available.

Message:   [b]Group not found[/b]
Server:   Alfresco Community Network v2.1.0 (R1 443) schema 62
Time:   Dec 13, 2007 3:35:02 PM

Diagnostics:   Inspect Web Script (org/EOC/AddUserToGroup.get)

That means getGroup returned undefined.

That is the problem. Now I'm going to say what else I saw.

While trying to make it work, I added another user to the group "Gerentes" and asked javascript what groups that user owned to, and the answer was:
0f512522-9f92-11dc-b07e-f5d358f2ab67
That's the name and group id of the group "Gerentes". Each time I create a group and ask its name, the answer is something similar.

I downloaded a nightbuild (alfresco 2.9) and tried the same (thinking it could be a bug), but it failed too. Anyway, as the new version brings a group in its database (EMAIL_CONTRIBUTORS) i tried to add a user to that group using webscript. And it worked!!.
The main difference between that group and another created by me was that the group alfresco brought had as group id EMAIL_CONTRIBUTORS, the same as the name displayed in alfresco webclient admin console and not an encrypted style name.

Any help will be accepted :cry:
Thanks
5 REPLIES 5

mikeh
Star Contributor
Star Contributor
Hi

Try "GROUP_" in front of the group name.

Thanks,
Mike

grosisimo
Champ in-the-making
Champ in-the-making
I have already tried it and it didn't work.
I put GROUP_Gerentes, and it threw the same error. Besides, when i asked for its name using the api, it had a "strange name" too.

mikeh
Star Contributor
Star Contributor
Hi

That "strange name" is a nodeRef and means your script worked as expected.

To get the actual group name from a usr:authorityContainer node (which is what the person.getGroup() function returns) use:
person.getGroup("GROUP_Gerentes").properties["usr:authorityName"]
which will give you back the "GROUP_Gerentes" string you passed-in.

You can use the Node Browser (as the admin user) to view these properties and also the Javascript debugger to check return types, etc.

Thanks,
Mike

grosisimo
Champ in-the-making
Champ in-the-making
Smiley Very Happy  Smiley Very Happy  Smiley Very Happy  Smiley Very Happy  Smiley Very Happy  Smiley Very Happy  Smiley Very Happy  Smiley Very Happy  Smiley Very Happy  Smiley Very Happy
It worked!

I still have to make a lot of tests, but the script added the user to the group.

Just in case someone else needs it, the only difference from the code written above is that the group had to be GROUP_Gerentes.
Maybe when I tried "GROUP_Gerentes" last time, the script had another changes, and those changes caused the errors.

Thanks

grosisimo
Champ in-the-making
Champ in-the-making
Finally I understood why it failed.

What I didn't know is that if the group name was "my group name", the name I had to use to reference it in the javascript api was "GROUP_my group name", that is "GROUP_<name of the group>".

Thanks