question

GalOfer-7756 avatar image
0 Votes"
GalOfer-7756 asked saldana-msft edited

What is the best way to use @microsoft/microsoft-graph-client inside SPFx web part?

@microsoft/microsoft-graph-client 3.0 has a client object that is instantiated with MSAL in all the examples.
But SPFx has a client too that can be instantiated using the current session by using

 import { MSGraphClient as SPFxGraphClient } from '@microsoft/sp-http';
 const client: SPFxGraphClient = await context.msGraphClientFactory.getClient();

I tried to use this client with microsoft-graph-client methods but they are not compatible.

Is there a way to cast SPFxGraphClient to the one in microsoft-graph-client?

Thank you.

sharepoint-devmicrosoft-graph-sdk
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MichaelHan-MSFT avatar image
0 Votes"
MichaelHan-MSFT answered MichaelHan-MSFT commented

Hi @GalOfer-7756,

No, you can't. MSGraphClient wraps the microsoft-graph-client library, but they are two different libraries.

To use the microsoft-graph-client, you have to instantiate the client like this:

 import {
   UserAgentApplication,
   Configuration,
   AuthenticationParameters,
 } from 'msal'
    
 import {
   MSALAuthenticationProviderOptions,
   ImplicitMSALAuthenticationProvider,
   Client,
   ClientOptions,
 } from '@microsoft/microsoft-graph-client'
    
   const msalConfig: Configuration = {
     auth: {
       clientId: "client_id",
     }
   };
   const graphScopes = [
     "user.read",
   ];
   const msalApplication = new UserAgentApplication(msalConfig);
   const msalOptions = new MSALAuthenticationProviderOptions(graphScopes);
   const authProvider = new ImplicitMSALAuthenticationProvider(msalApplication, msalOptions);
    
   const authParams: AuthenticationParameters = {
     scopes: graphScopes,
     prompt: 'select_account',
   };
       /* uncomment to switch account
   await msalApplication.loginPopup(authParams);
   */
   const options: ClientOptions = {
     authProvider,
   };
    
   const client = Client.initWithMiddleware(options);


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.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@GalOfer-7756,
Is there anything update on this issue?

0 Votes 0 ·
GalOfer-7756 avatar image
0 Votes"
GalOfer-7756 answered

If I can't use the web prat context, it is useless to me.
This, and the other short comings of Graph, tell me to stop using graph for SharePoint actions.

Thank you

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.