Home > Enterprise >  People.People.searchDirectoryPeople() query with multiple email addresses
People.People.searchDirectoryPeople() query with multiple email addresses

Time:05-27

In a Google Apps Script I need to query the Google user profile picture URL of many coworkers.

Here is a working example for a single user:

searchDirectoryPeople('[email protected]');

function searchDirectoryPeople(query) {
  const options = {
    query: query,
    readMask: 'photos,emailAddresses',
    sources: ['DIRECTORY_SOURCE_TYPE_DOMAIN_PROFILE']
  }
  const people = People.People.searchDirectoryPeople(options);
  if(people && people.people) {
    Logger.log('size: ' people.people.length);
    people.people.forEach(person => {
      let url = '';
      let email = '';
      if(person) {
        if(person.photos && person.photos[0]) {
          url = person.photos[0].url;
        }
        if(person.emailAddresses && person.emailAddresses.length) {
          person.emailAddresses.forEach(item => {
            if(item.metadata && item.metadata.sourcePrimary) {
              email = item.value;
            }
          });
        }
      }
      Logger.log('email: ' email ': ' url);
      //Logger.log('person: %s', JSON.stringify(person, null, 2));
    });
  } else {
    Logger.log('no people.people');
  }
}

I found out that I can query all jimmy people:

searchDirectoryPeople('jimmy');

I have the email address of all employees. I could loop through a big list of 1000 employees one by one, but this is not practical. I am looking for a way to query multiple email addresses. The docs at https://developers.google.com/people/api/rest/v1/people/searchDirectoryPeople are cryptic for the query. I tried many things like these but nothing works:

I am looking for a query by list of email addresses as input, such as:

[ '[email protected]', '[email protected]', '[email protected]' ]

Is it possible to have an OR query in People.People.searchDirectoryPeople()?

CodePudding user response:

How about this?

function searchDirectoryPeople(query) {
  const options = {
    query: query,
    readMask: 'photos,emailAddresses',
    sources: ['DIRECTORY_SOURCE_TYPE_DOMAIN_PROFILE']
  }
  const people = People.People.searchDirectoryPeople(options);
  if(people && people.people) {
    Logger.log('size: ' people.people.length);
    people.people.forEach(person => {
      let url = '';
      let email = '';
      if(person) {
        if(person.photos && person.photos[0]) {
          url = person.photos[0].url;
        }
        if(person.emailAddresses && person.emailAddresses.length) {
          person.emailAddresses.forEach(item => {
            if(item.metadata && item.metadata.sourcePrimary) {
              email = item.value;
            }
          });
        }
      }
      return {"imgurl":url,"email":email}
    });
  } 
}

function searchPlus(emailArray) {
  let oA = [];
  emailArray.forEach(e => {
    oA.push(searchDirectoryPeople(e))
  });
  if(oA && oA.length) {
    return oA;
  }
}

CodePudding user response:

It looks like with Method: people.searchDirectoryPeople you can only specify one person at a time.

Another option could be People.People.getBatchGet() which will require an extra step but provides you information about a list of the people you specify. The request would look something like this:

const options = {
    personFields: 'photos,emailAddresses',
    resourceNames: [
      'people/account_id',
      'people/account_id',
      'people/account_id'
    ],
    sources: [
      'READ_SOURCE_TYPE_PROFILE'
    ]
  }
  
  const people = People.People.getBatchGet(options);

You can get the user's account_id with Method: people.listDirectoryPeople

  • Related