pnp / pnpjs

Fluent JavaScript API for SharePoint and Microsoft Graph REST APIs
https://pnp.github.io/pnpjs/
Other
742 stars 302 forks source link

Getting TypeError: Cannot read properties of undefined (reading 'aadTokenProviderFactory') when using sp pnp graph #2987

Closed cardinalpipkin closed 3 months ago

cardinalpipkin commented 3 months ago

What version of PnPjs library you are using

3.x

Minor Version Number

24.0

Target environment

SharePoint Framework

Additional environment details

 "webApiPermissionRequests": [
      {
        "resource": "Microsoft Graph",
        "scope": "Sites.Read.All"
      },
      {
        "resource": "Microsoft Graph",
        "scope": "Group.Read.All"
      },
      {
        "resource": "Microsoft Graph",
        "scope": "User.ReadBasic.All"
      },
      {
        "resource": "Microsoft Graph",
        "scope": "User.Read.All"
      },
      {
        "resource": "Microsoft Graph",
        "scope": "User.Sites.All"
      }
    ],

Have checked that all WebParts have the required permissions within admin

Question/Request

I'm attempting to get the Id of an SPO Hub site by using both getById and getByUrl:

import { getSP, getGraph } from '../pnpjs-config';
import { SPFx } from "@pnp/sp";
import "@pnp/sp/webs";
import "@pnp/sp/lists";
import "@pnp/sp/sites";
import "@pnp/graph/sites";
import "@pnp/graph";
import "@pnp/sp/items";
import "@pnp/sp/comments"
import "@pnp/sp/site-users/web";
import "@pnp/sp/attachments";
import "@pnp/sp/folders";
import "@pnp/sp/files";
import "@pnp/sp/site-groups/web";
import { IHubSiteInfo } from '@pnp/sp/hubsites';
import "@pnp/sp/hubsites";
import "@pnp/sp/sites/types";
import { SearchResults } from "@pnp/sp/search";
import "@pnp/sp/search";
import { MSGraphClientV3 } from "@microsoft/sp-http";
import { Web } from '@pnp/sp/webs';
import { WebPartContext } from '@microsoft/sp-webpart-base';

export const GetHubSiteByIdMSGraphSPPnP = async (context: WebPartContext, hubUrl: string) => {
  const _graph = getGraph();
  const removeText: string = hubUrl.substring(31);
  const getByIdSite = await _graph.sites.getById('c2b19267-2425-483f-91a7-7cd833e2d8cd')()
  console.log(getByIdSite, 'getByIdSite')
  const resultHub = await _graph.sites.getByUrl("myOrg.sharepoint.com", removeText)();
  console.log(resultHub, 'resultHub');
};

and I'm getting the same error for both attempts above.

I'm using the config file to get context:

import { WebPartContext } from "@microsoft/sp-webpart-base";
import { spfi, SPFI, SPFx as spSPFX } from "@pnp/sp";
import { LogLevel, PnPLogging } from "@pnp/logging";
import { graphfi, GraphFI, SPFx as graphSPFx} from "@pnp/graph";
import "@pnp/sp/webs";
import "@pnp/sp/lists";
import "@pnp/sp/items";
import "@pnp/sp/batching";
import "@pnp/graph";
import "@pnp/graph/sites";
import "@pnp/graph/lists";
import "@pnp/graph/members";
import "@pnp/graph/users";

//@ts-expect-error this is due to esLint being overactive
let _sp: SPFI = null;
//@ts-expect-error this is due to esLint being overactive
let _graph: GraphFI = null;
export const getSP = (context?: WebPartContext): SPFI => {
  if (_sp === null && context !== null) {
   //@ts-expect-error blerb
    _sp = spfi().using(spSPFX(context)).using(PnPLogging(LogLevel.Warning));
  }
  return _sp;
};
export const getGraph = (context?: WebPartContext): GraphFI => {
  if (_graph === null && context !== null) {
  //@ts-expect-error blerb
    _graph = graphfi().using(graphSPFx(context)).using(PnPLogging(LogLevel.Warning));
  }
  return _graph;
};

The getSP works as expected but not getGraph. Am I missing something obvious here?

patrick-rodgers commented 3 months ago

I am guessing that your context is undefined which would be easy to determine by debugging.

cardinalpipkin commented 3 months ago

Yes - that was it. I hadn't added the context correctly. Thank you as ever.

  protected async onInit(): Promise<void> {

    super.onInit();

    //Initialize our _sp object that we can then use in other packages without having to pass around the context.
    //  Check out pnpjsConfig.ts for an example of a project setup file.
    getSP(this.context);
    getGraph(this.context); <----I hadn't added this....
  }
github-actions[bot] commented 3 months ago

This issue is locked for inactivity or age. If you have a related issue please open a new issue and reference this one. Closed issues are not tracked.