cancel
Showing results for 
Search instead for 
Did you mean: 

How to create group and add users to group using WebService?

madhurao
Champ in-the-making
Champ in-the-making
Hi Friends,
I am new Alfresco.I am using Web Service(WS) api for my development. Using WS api I can create space, create a user, upload file, create file and assign role to the user. But i could not able to find out any methods to create Group and add users to groups. If you have snippet of code, please share which helps to me understand very quickly.its very urgent .

Regards,
Madhu
19 REPLIES 19

mrogers
Star Contributor
Star Contributor
Don't know about web services.   

But these methods are available as Web Scripts in 3.2.

madhurao
Champ in-the-making
Champ in-the-making
I have searched for Web scripts API but I could not find out. Can you please send me url for Web Scripts API?


Regards,
Madhu

kayan
Champ in-the-making
Champ in-the-making
Hey Madhu,
Try the following webscript. It should help you to add users to the group

var groupPrefix="GROUP_";
var srcGrpNode=people.getGroup(groupPrefix+srcGrp);
var authority = people.getPerson(user);
if(authority)
{
   people.addAuthority(srcGrpNode,authority);
}

Have a look into people API section you can also find method to create the group.

http://wiki.alfresco.com/wiki/JavaScript_API_For_Alfresco_2.1#People_API

Thanks
Kayan

mrogers
Star Contributor
Star Contributor
Yes you can use the people web script and Java Script API

And there's also two sets of webscripts on
/api/groups
/api/rootgroups

And there is a java script "groups" object.

erolozcan
Champ in-the-making
Champ in-the-making
I am sending my  testing code snippet to create group and add users via Web service interfaces.


   …
    public static final String    ADMIN_USERNAME             = "admin";
    public static final String    ADMIN_PASSWORD             = "admin";

    public static final String       GROUP_AUTHORITY_TYPE       = "GROUP";

    public static final String    CP01_USERNAME              = "cp01";

    protected static final String    CONTENT_PROVIDER_GROUPNAME = "RBT_CONTENT_PROVIDER";

    public static void main(String[] args) throws Exception {
        // Start the session
        AuthenticationUtils.startSession(ADMIN_USERNAME, ADMIN_PASSWORD);
        try {
            createGroups();
            //createUsers();
            addUsersToGroup();
        } finally {
            // End the session
            AuthenticationUtils.endSession();
        }
    }

    private static void createGroups() throws AccessControlFault, RemoteException {
        AccessControlServiceSoapBindingStub accessControlService = WebServiceFactory.getAccessControlService();

        NewAuthority cpGrpAuth = new NewAuthority(GROUP_AUTHORITY_TYPE, CONTENT_PROVIDER_GROUPNAME);
        NewAuthority[] newAuthorities = {cpGrpAuth};
        String result[] = accessControlService.createAuthorities(null, newAuthorities);
    }

    private static void addUsersToGroup() throws AccessControlFault, RemoteException {
        String[] cpUsers = {CP01_USERNAME};
        String parentAuthority  = GROUP_AUTHORITY_TYPE + "_" + CONTENT_PROVIDER_GROUPNAME;

        AccessControlServiceSoapBindingStub accessControlService = WebServiceFactory.getAccessControlService();

        String[] result = accessControlService.addChildAuthorities(parentAuthority,
                                                                   cpUsers);
    }
   …

spizzico7
Champ in-the-making
Champ in-the-making
hi,
i have a project and i must use Alfresco for a gestion of documental.
I use java for interface Alfresco with my project.
Now i have create a method for create authority and teh the result is ok, i have use your code for add user to group and create a new group.
I need a code for create user and delete group.
For the second i used this code:

public static boolean deleteGroups(String groupname) {
          boolean control=true;
          try{
             String[] delgroup = {groupname};
          /*   String parentAuthority  = GROUP_AUTHORITY_TYPE + "_" + groupname;

             AccessControlServiceSoapBindingStub accessControlService = WebServiceFactory.getAccessControlService();
             NewAuthority cpGrpAuth = new NewAuthority(GROUP_AUTHORITY_TYPE,groupname);
               NewAuthority[] newAuthorities = {cpGrpAuth};
   
           accessControlService.deleteAuthorities(delgroup);*/
      
          

          }
but a group doesn't delete.
For the first i have search a code and try to write but no result.
Please help.

spizzico7
Champ in-the-making
Champ in-the-making
I have resolved for the create user.

Now I must delete a group….How?
Help! :cry:

rliu
Champ in-the-making
Champ in-the-making
First you have to obtain the node reference to the user group.

Once you've obtained it, then you can use the NodeService.deleteNode(NodeRef nodeRef).

Remember, this is a cascading delete as explained in the JavaDoc, so make sure that you want all child nodes to be deleted before invoking this call.

dynamolalit
Champ on-the-rise
Champ on-the-rise
Hi Madhu,

Can you please let me know how to assign a role to a user.I am able to create a user in alfresco but struggling with assigning him a role  :cry: .Here is the source for the same:

private AuthenticationService authenticationService;
   private PersonService personService;
   ManageTaxonomyHelper helper;
   public void execute(WebScriptRequest req, WebScriptResponse res)
         throws IOException {      
      String userName = req.getParameter("userName");
      String firstName = req.getParameter("firstName");
      String lastName = req.getParameter("lastName");
      String password = req.getParameter("password");
      String emailId = req.getParameter("emailId");
      helper = new ManageTaxonomyHelper();
      this.authenticationService = helper.getAuthenticationService();
      this.personService = helper.getPersonService();                  
      if (this.authenticationService.authenticationExists(userName) == false) {
         this.authenticationService.createAuthentication(userName, password.toCharArray());
         PropertyMap propMap = new PropertyMap(3);         
         propMap.put(ContentModel.PROP_USERNAME, userName);
         propMap.put(ContentModel.PROP_FIRSTNAME, firstName);
         propMap.put(ContentModel.PROP_LASTNAME, lastName);
         propMap.put(ContentModel.PROP_EMAIL, emailId);         
         this.personService.createPerson(propMap);//Creating a new user here.
      }else{
         System.out.println("User already exists");
      }

Thanks in advance.