1

I was trying to set up an instance of client so I don't have to write this.context.msGraphClientFactory.getClient().then((client: MSGraphClient) every time I want to make a request.

Something which translates to what they set up here in step 2 : https://github.com/microsoftgraph/msgraph-sdk-javascript

I've tried doing at as below in my WebParts.ts file, but I don't get it to work;

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

export interface IGraphConsumerWebPartProps {
  description: string;
}

export default class GraphConsumerWebPart extends BaseClientSideWebPart<IGraphConsumerWebPartProps> {

  private _isDarkTheme: boolean = false;
  private _environmentMessage: string = '';
  private graphClient: MSGraphClient;
  private _clientInstance: any;

  // My attempt to create a client instance on init for the lifecycle of the class
  protected onInit(): Promise<void> {
    //return new Promise<void>((environvars) => {
    //this._environmentMessage = this._getEnvironmentMessage();
    this._clientInstance = this._getClientInstance();
    //});
    return this._clientInstance;
    
  }

  public render(): void {
    const element: React.ReactElement<IGraphConsumerProps> = React.createElement(
      GraphConsumer,
      {
        clientMode: this.properties.clientMode,
        context: this.context,
        description: this.properties.description,
        isDarkTheme: this._isDarkTheme,
        environmentMessage: this._environmentMessage,
        hasTeamsContext: !!this.context.sdks.microsoftTeams,
        userDisplayName: this.context.pageContext.user.displayName
      }
    );
    

    // This successfully runs and logs the user
    this.context.msGraphClientFactory.getClient().then((client: MSGraphClient): void => {
      // get information about the current user from the Microsoft Graph
      client.api('/me').get((error, user: any, rawResponse?:any)=> {
        console.log("user is working");
        console.log(user);

        
        // get information about the current user from the Microsoft Graph
      });
    });
    console.log(this._clientInstance)
    console.log("testing");

    // This fails
    this._clientInstance.then((client: MSGraphClient): void => {
      // get information about the current user from the Microsoft Graph
      client.api('/me').get((error, user: any, rawResponse?:any)=> {
        console.log("user is cool");
        console.log(user);

      });
    });
    
    ReactDom.render(element, this.domElement);
  }

  // How I try to create the client instance
  private _getClientInstance(): Promise<void>{    
    return new Promise<void>((resolve: () => void, reject: (error: any) => void): void => {
      this.context.msGraphClientFactory.getClient().then(
          (client: MSGraphClient): void => {
            this.graphClient = client;
            resolve();
          }, err => reject(err));
    });
  }


5
  • Check this example, let me know if it works for you. Jun 17, 2022 at 7:00
  • I am not getting it to work I'm afraid
    – OLGJ
    Jun 17, 2022 at 7:16
  • But I don't know if that is what I'm trying to achieve? I am doing my communcation internally via user context through sharepoint and communicate to Graph this way. So I guess via await this.context.msGraphClientFactory.getClient()?
    – OLGJ
    Jun 17, 2022 at 7:32
  • Just a guess but I'm thinking you're not binding the _getClientInstance function correctly which is causing the this.graphClient = portion to not set the right stuff. Try switching the signature to an arrow function: private _getClientInstance = (): Promise<void> => { and see if that helps. Also, move the fetch logic out of render and put it in the then during the init. You don't want to get data every time you render and then rerender because of new data leading to getting new data, etc. You also aren't returning the client in the resolve, but you expect it as a parameter in then Jun 17, 2022 at 15:04
  • The switch to an arrow function seemed to do the trick! What do you mean with moving the fetch logic out of the render? I have it in the onInit already?
    – OLGJ
    Jun 20, 2022 at 6:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.