2

I am using Sharepoint Framework(SPFX) and using Typescript for calling REST APIs. I want to get information of other employees from Office 365 or Active Directory using their Email ID or Employee ID.

How can I do this with REST API in Sharepoint Framework(SPFX)

1

Depending on the properties you would like to access, you could pull this from the SharePoint Search REST API

Another option is using the Microsoft Graph API. You can play with that using the cool Graph Explorer and see what the different calls will provide you with. For example, this one will give you details about a specific user account:

 https://graph.microsoft.com/v1.0/users/first.last@email.com

This one will require some extra steps to request permissions to access the API itself, read about that here

0

I hope this method will help. It retrieves a list with expanded Userinformation. You need to extend a Person/Group Field (like Created by) when retrieving list items, then you can get user ID, Mail, Department and so on.

import { sp, Web, List} from "@pnp/sp";

public getItemsExpandedUser(listTitle: string, selection: string[], expandField: string): Promise<any> {
if (this.createOnFail) {
  return this.ensureList().then((list: List): Promise<any> => {
    return this.web.lists.getByTitle(listTitle).items.select(...selection).expand(expandField).getAll().then(result => {
      return result;
    }).catch((error: any) => {
      console.error(error);
      return error;
    });
  });
} else {
  return this.web.lists.getByTitle(listTitle).items.select(...selection).expand(expandField).getAll().then(result => {
    return result;
  }).catch((error: any) => {
    console.error(error);
    return error;
  });
}

}

An example call could be like this (in Order to get more information of a Person/Group field called 'Employee'):

[...] this.getItemsExpandedUser(**YOUR LISTNAME**, ['Id', 'Title', 'Employee', 'Employee/Id', 'Employee/Title', 'Employee/EMail'], 'Employee/Id').then(result => { DO SOMETHING });
2
  • Hello, I dont want to get it from list. I want to get it from Office 365. – karan sharma Dec 20 '19 at 10:17
  • oh sorry my fault – MP_89 Dec 20 '19 at 10:19
0

ok then. I hope this one will do it ;-)

This method gets all siteUsers:

  //get all users with access to the sitecollection
  get fetchSiteUsers(): Promise<IPersona[]> {
    return this.getWeb().siteUsers.get().then(spUsers => {
      console.log(spUsers);
      return spUsers;
    });
  }

alternatively you can retriev a single User by using

this.getWeb().getUserById(*id*) 

or for the active logged in user

this.getWeb().currentUser.get()

if this all is not what you are looking for I'm sorry for taking your time ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.