Search code examples
google-app-maker

How to get users first and last name?


@user.email seems to instantly print email address of a user that is using the app on any widget. Is there a way to get User Name (First Last) in a similar way?


Solution

  • The solution I usually use involves using the directory model. Follow these steps:

    1.) Enable the directory model by creating it, I usually name it directory

    enter image description here

    2.) Next, go to the DATASOURCES section of the directoy model you just created and click on the ADD DATASOURCE button. I usually name this datasource activeUser.

    enter image description here

    3.) Add the following to any server script:

    function getActiveUser() {    
      var dirQuery = app.models.directory.newQuery();
      dirQuery.filters.PrimaryEmail._equals =  Session.getActiveUser().getEmail();
      var userResult = dirQuery.run();
      return userResult;     
    }
    

    4.) Go back to the activeUser datasource. Add the following to the Server Script Query:

    return getActiveUser();
    

    And change the query page size to 1.

    enter image description here

    5.) Now all you have to make sure is that the widget where you will print the user name has the datasource activeUser either directly or inherited. Then you simply bind it to the FullName property.

    enter image description here