using microsoft graph to get user profile properties from azure and O365 groups on a SPFX webpart

2025-01-23T13:43:51.2933333+00:00

I been trying to get information from the Azure AD using the code below, but the result is Error fetching user profile undefined

I am running the webpart with gulp serve

on my serve.json file I have my sharepoint site collection url so it opens the workbench with the sitecollection url

import * as React from 'react';
import { IMyWebPartProps } from './IMyWebPartProps';

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

export interface IMyWebPartState {
userTitle: string | null;

error: string | null;

apiData: any | null;

users: Array<IUserItem>;

}

let displayNames='Doe, Jon';

export default class MyWebPart extends React.Component<IMyWebPartProps, IMyWebPartState> {

constructor(props: IMyWebPartProps) {

super(props);

this.state = {

  userTitle: null,

  error: null,

  apiData: null,

  users: []

};
```  }

  componentDidMount() {

this.getUserProfiles2();


  public async getUserProfiles2(): Promise<void> {

// Use msGraphClientFactory to get the MSGraphClientV3 instance


.getClient('3') // This retrieves an instance of MSGraphClientV3

.then(async (client: MSGraphClientV3) => {

// Use Promises and then() for the API call to avoid callback-based errors

client.api('/me')

 .version('v1.0')  // You can specify the API version here

 .get(async (err,res)=>{

       if (err){

         console.error("error ",err);

         return

       }

       console.log("res: ",res.value)

       return res.value  

 })

 .then(async (user: MicrosoftGraph.User) => {

   // Handle the successful response here

   console.log("User:", user);

 })

 .catch((error: any) => {

   // Handle error if the API request fails

   console.error('Error fetching user profile:', error);

 });

})

.catch((error: any) => {

// Handle errors in initializing MSGraphClientV3

console.error('Error initializing MSGraphClientV3:', error);

});


  public render(): React.ReactElement<IMyWebPartProps> {

const { userTitle, error } = this.state;

return (

<div>

<h1>Testing</h1>

</div>

);


 }

on my package-solution.json I have added:

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

{

"resource": "Microsoft Graph",

"scope": "GroupMember.Read.All"

},

{

"resource": "Microsoft Graph",

"scope": "Group.Read.All"

},

{

"resource": "Microsoft Graph",

"scope": "Directory.Read.All"

}

]

Microsoft 365
Microsoft 365
Formerly Office 365, is a line of subscription services offered by Microsoft which adds to and includes the Microsoft Office product line.
5,583 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,894 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.
3,229 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
23,041 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.