none
GraphHttpClient is undefined RRS feed

  • Question

  • Hello,

    I was trying to fetch current user details using graph api like this -

    import { GraphHttpClient, HttpClientResponse, IGraphHttpClientOptions } from '@microsoft/sp-http';

     this.props.context.graphHttpClient.get(`v1.0/me`, GraphHttpClient.configurations.v1).then((response: HttpClientResponse) => {
          if (response.ok) {
            return response.json();
          } else {
            console.warn(response.statusText);
          }
        })


    which was working fine with SPFx 1.4.1. But suddenly my code stop working and now it is giving error as Cannot read property 'get' of undefined  for graphHttpClient. Here, graphHttpClient is undefined.

    Please guide me for this. I don't want to change my code using MSGraphClient is there any other shortcut solution?

    Thanks in advance.

    Friday, October 18, 2019 12:30 PM

Answers

  • Hi Ashwini,

    If you are build with SPFX v1.6.0 and later, please call call Graph API like this:

    1. Install the Graph API TypeScript:

    npm install @microsoft/microsoft-graph-types --save-dev


    2. Import using this line:

    import { MSGraphClient } from '@microsoft/sp-http';


    3. Get Current User details:

      public render(): void {
        this.context.msGraphClientFactory
        .getClient()
        .then((client: MSGraphClient): void => {
          // get information about the current user from the Microsoft Graph
          client
            .api('/me')
            .get((error, response: any, rawResponse?: any) => {
              // handle the response
              console.log(response);
          });
        });
      }

    This is the response from Graph API in Visual Studio Code Console in my side:

    Reference:

    Use the MSGraphClient to connect to Microsoft Graph

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, October 21, 2019 6:20 AM

All replies

  • Hi Ashwini,

    If you are build with SPFX v1.6.0 and later, please call call Graph API like this:

    1. Install the Graph API TypeScript:

    npm install @microsoft/microsoft-graph-types --save-dev


    2. Import using this line:

    import { MSGraphClient } from '@microsoft/sp-http';


    3. Get Current User details:

      public render(): void {
        this.context.msGraphClientFactory
        .getClient()
        .then((client: MSGraphClient): void => {
          // get information about the current user from the Microsoft Graph
          client
            .api('/me')
            .get((error, response: any, rawResponse?: any) => {
              // handle the response
              console.log(response);
          });
        });
      }

    This is the response from Graph API in Visual Studio Code Console in my side:

    Reference:

    Use the MSGraphClient to connect to Microsoft Graph

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, October 21, 2019 6:20 AM
  • Hi Jerry,



    Thanks for the solution. But as I mentioned I'm working on SPFx v1.4.1 and because of other dependencies I could not upgrade this version.

    So how I can make the thing work for the same version? will your solution work for v1.4.1?


    Monday, October 21, 2019 6:53 AM
  • Hi Ashwini,

    Yes,  In versions of the SharePoint Framework starting with v.1.4.1, you can access Microsoft Graph by using either the native graph client (MSGraphClient), please check the description below:

    Reference:

    Consume the Microsoft Graph in the SharePoint Framework

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, October 22, 2019 7:26 AM
  • Hi Jerry,

    I would like to tell you that, my issue has been resolved by using MSGraphClient. 

    Along with that, we need to Configure the API permissions requests. Follow given link for the same : 

    https://docs.microsoft.com/en-us/sharepoint/dev/spfx/use-aad-tutorial#ConfiguringApiPermissions

    Thank you.

    Wednesday, October 23, 2019 2:37 PM
  • Hi Ashwini,

    Upgrading may work. But what about the application which we deployed one year back. 

    One of my client reported that the pages are not working and i realised the GraphHttpClient is undefined.

    Is there any workaround without migrating to latest codebase?

    Regards
    Nirmal

    Sunday, November 10, 2019 4:52 AM