tslint error "Cannot find namespace 'MicrosoftGraph'. ts(2503)" in SPFx web Part

Gal, Ofer 81 Reputation points
2021-04-06T16:00:49.51+00:00

I followed all the instructions
npm install @microsoft/microsoft-graph-client --save
npm install @microsoft/microsoft-graph-types --save-dev

then in code

import "isomorphic-fetch";
import { Client } from "@microsoft/microsoft-graph-client";

and

  try {
    const uploadTask: MicrosoftGraph.OneDriveLargeFileUploadTask =
      await MicrosoftGraph.OneDriveLargeFileUploadTask.create(client, file, options);
    const uploadedFile: DriveItem = await uploadTask.upload();
    console.log(JSON.stringify(`Uploaded file with ID: ${uploadedFile.id}`));
    return `Uploaded file with ID: ${uploadedFile.id}`;
  } catch (err) {
    console.log(`Error uploading file: ${JSON.stringify(err)}`);
    return `Error uploading file: ${JSON.stringify(err)}`;
  }

the TSLINT errors:"Cannot find namespace 'MicrosoftGraph'. ts(2503)"

Also, how do I get a Client object from props (MSGraphClientFactory) I get from the Web Part?

Thank you much

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,565 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Amos Wu-MSFT 4,051 Reputation points
    2021-04-07T02:09:21.23+00:00

    Hi @Gal, Ofer ,
    Judging according to the error message, you did not import the MicrosoftGraph module in the code, please try to import the module in the code.

    import * as MicrosoftGraph from "@microsoft/microsoft-graph-types"  
    

    If you are developing SPFX, you may have a look at the MSGraphClient.MSGraphClient is a new HTTP client introduced in SharePoint Framework v1.6.0 that simplifies connecting to the Microsoft Graph inside SharePoint Framework solutions.
    Use the MSGraphClient to connect to Microsoft Graph


    If the response 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. Gal, Ofer 81 Reputation points
    2021-04-07T21:24:04.123+00:00

    I figured out that the example code is wrong:

    const uploadTask: MicrosoftGraph.OneDriveLargeFileUploadTask = await MicrosoftGraph.OneDriveLargeFileUploadTask.create(client, file, options);

    Should be:

    import {Client, OneDriveLargeFileUploadTask} from "@microsoft/microsoft-graph-client"
    const uploadTask: OneDriveLargeFileUploadTask = await OneDriveLargeFileUploadTask.create(client, file, options);

    But I have trouble converting MSGraphClient } from "@microsoft/sp-http" that is provided by SPFx, to Client from "@microsoft/microsoft-graph-client"

    Anyone can help?