Retrieve all users properties from User Profile (for SharePoint Online )

NZ 21 Reputation points
2021-02-04T15:13:43.997+00:00

Hello,

I need some guidance regarding how to retrieve all my users properties using Graph API or any other API that would work in SPFx environment. I have retrieved some properties but the image URL of the user was the problem.

Note: I have tried to use static URL for the image and imbed user account in it but it didn't work properly for all users.

Appreciate your help.

Best Regards

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,569 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,664 questions
0 comments No comments
{count} votes

Accepted answer
  1. MichaelHan-MSFT 18,016 Reputation points
    2021-02-05T09:34:13.403+00:00

    Hello @NZ ,

    You could use graph api to get all users, then loop through the users, use @pnp/sp/profiles to get user properties for each user. Below is my sample code for you:

    this.context.msGraphClientFactory  
    .getClient()  
    .then((client: MSGraphClient): void => {  
      client.api('/users').get(async (error, users: any, rawResponse?: any) => {  
          //console.log(users.value);  
          for(let user of users.value){  
            console.log(user.userPrincipalName);  
            let loginName="i:0#.f|membership|"+user.userPrincipalName;  
            const profile = await sp.profiles.getPropertiesFor(loginName);  
            console.log(profile);   
          }  
      });  
    });  
    

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. MichaelHan-MSFT 18,016 Reputation points
    2021-02-05T07:31:42.853+00:00

    Hi @NZ ,

    You could use @pnp/sp/profiles to get user properties in SPFX solution.

    import { sp } from "@pnp/sp";  
    import "@pnp/sp/profiles";  
    

    Establish Context:

      protected async onInit(): Promise<void> {  
      
        await super.onInit();  
        
        // other init code may be present  
        
        sp.setup(this.context);  
      }  
    

    Get the current user profile:

    const profile = await sp.profiles.userProfile;  
    console.log(profile);  
    

    Get a specific user profile:

    const loginName = "i:0#.f|membership|testuser@mytenant.onmicrosoft.com";  
    const profile = await sp.profiles.getPropertiesFor(loginName);  
    console.log(profile);   
    console.log(profile.PictureUrl);   
    

    Test result:

    64410-image.png


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. MichaelHan-MSFT 18,016 Reputation points
    2021-02-05T09:06:15.48+00:00

    Hello @NZ ,

    If you want to get "all users" properties in spfx, you need to use graph api to achive this: https://learn.microsoft.com/en-us/graph/api/user-list?view=graph-rest-1.0&tabs=csharp

    In ./config/package-solution.json file, request permissions for the User.Read.All scope to get all users

    "webApiPermissionRequests": [  
      {  
        "resource": "Microsoft Graph",  
        "scope": "User.Read.All"  
      }  
    ]  
    

    64483-image.png

    Import statement:

    import { MSGraphClient } from '@microsoft/sp-http';  
    import * as MicrosoftGraph from '@microsoft/microsoft-graph-types';  
    

    List all users properties:

    this.context.msGraphClientFactory  
    .getClient()  
    .then((client: MSGraphClient): void => {  
      client.api('/users').get((error, user: MicrosoftGraph.User, rawResponse?: any) => {  
          console.log(user);  
    
      });  
    });  
    

    Test Result:

    64378-image.png

    Reference:

    https://learn.microsoft.com/en-us/sharepoint/dev/spfx/use-msgraph
    https://learn.microsoft.com/en-us/sharepoint/dev/spfx/web-parts/get-started/using-microsoft-graph-apis


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.