Search code examples
google-app-maker

Google app maker: How to list existent groups?


I've created a new google app through their app maker interface. I've added "Google Admin Directory API" as a service and "Directory" as a data source. I created a page that will hold 2 tables, one that list all users within my domain (this is already working) and another table that lists all of the groups within my domain (not working). How can I achieve this? Can this be done through their widgets or do I have to create a script and programmatically call the admin API to then bind the data to the table?


Solution

  • Since you already enabled the Admin Directory API when using the directory model, all you have to do now is to call the sample code from the server script. In a server script, add the sample code:

    function listAllGroups() {
      var pageToken;
      var page;
      do {
        page = AdminDirectory.Groups.list({
          domain: 'example.com',
          maxResults: 100,
          pageToken: pageToken
        });
        var groups = page.groups;
        if (groups) {
          for (var i = 0; i < groups.length; i++) {
            var group = groups[i];
            Logger.log('%s (%s)', group.name, group.email);
          }
        } else {
          Logger.log('No groups found.');
        }
        pageToken = page.nextPageToken;
      } while (pageToken);
    }
    

    Then you can simply call the server script by using the following in the client scripting:

    google.script.run.withSuccessHandler(function(response){
        console.log(response);
    }).withFailureHandler(function(err){
        console.log(err);
    }).listAllGroups();
    

    You can check the reference here. I hope this helps!