Search code examples
google-apps-scriptgoogle-contacts-api

Using ContactsApp to get email addresses


I'm working on a Google Apps Script which will notify a student's teachers when their name is selected for attendance notices (suspension, field trip, etc). I tried using ContactsApp to get teacher information from the Google Apps Directory, but according Google Apps Script - ContactsApp - Directory search, the directory isn't available through the class.

As a proof of concept, I added some contacts from the directory to my own library so I could access the information from a script. However, when I search for a user using their name, email address...anything, really, I get an empty or null return. I've even cut it down to a hard-coded query (rather than variables) to see if I can get anything to return.

function testing() {
  var user = ContactsApp.getContactsByEmailAddress('user@elkhart.k12.in.us');
  for(var i in user) {
    Logger.log(user[i].getGivenName());
  }
}

Putting in a valid email address - even my own - returns an empty Log entry. Any ideas on whether this is just a fussy Class in scripts or if I'm approaching this the wrong way?


Solution

  • Since you're looking for a solution that will run from a general domain user account, you are wise to avoid relying on the Admin SDK. As long as you add the necessary users to your contacts (or to the contacts for whatever account you intend to run your script from), you'll be fine.

    Your code works for me; I was able to find user matches from my own consumer account's contacts. Therefore, it's worth looking deeper at your source data; the content of your contacts may have simply typos, for example.

    Here I've modified your testing() function to dump information about the contents of your Contacts, and ultimately to look for all your contacts that share your domain, e.g. elkhart.k12.in.us. (Or gmail.com for a consumer account.) It logs some additional information that may help pinpoint an issue with your source data.

    /**
     * Get info about my contacts.
     *
     * @returns  Contact[]    Array of contacts in same domain as user.
     *
     * Adapted from //stackoverflow.com/questions/32477736
     */
    function getContactsInfo() {
      // Get all my contacts, just to validate the number.
      var contacts = ContactsApp.getContacts();
      Logger.log("Total contacts: %s",contacts.length.toString());
    
      // Get info about my Contact groups (includes System Groups)
      var myGroups = ContactsApp.getContactGroups();
      Logger.log("Contact Groups (%s): %s",
                 myGroups.length.toString(),
                 myGroups.map(function(group){    // from //stackoverflow.com/a/16607599/1677912
                   return group.getName();
                 }).join());
    
      // Get my domain from my email (from //stackoverflow.com/a/5181776/1677912)
      var myDomain = Session.getActiveUser().getEmail().split("@")[1];
      Logger.log("My domain: %s", myDomain);
    
      // Get all domain users in my contacts
      var users = ContactsApp.getContactsByEmailAddress(myDomain);
      Logger.log("Matches in domain: %s",users.length.toString());
      for(var i=0; i<users.length; i++) {
        Logger.log(users[i].getGivenName());
      }
      return users;
    }