pnp / pnpjs

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

Wrong contextinfo url when using fileFromAbsolutePath and fileFromPath #2939

Closed Perneel closed 5 months ago

Perneel commented 6 months ago

Major Version

3.x

Minor Version Number

23.0

Target environment

All

Additional environment details

I'm developing with the SPFX framework in the context of a user.

Expected or Desired Behavior

I'm trying to read a json file on another site (in this case the hubsite of the sitecollection I'm testing with).

Observed Behavior

For this I'm using fileFromAbsolutePath (also tried fileFromPath). I noticed that when using an absolute url to another sitecollection, the endpoint for receiving the contextinfo is wrong.

For example: fileUrl = https://_tenant_.sharepoint.com/sites/hubsite/ConfigDocumenten/global.config.json

I notice that the contextinfo url is as follows: https://_tenant_.sharepoint.com/sites/hubsite/ConfigDocumenten/global.config.json/_api/contextinfo instead of https://_tenant_.sharepoint.com/sites/hubsite/_api/contextinfo

This throws a HTTP 404 (Not found) error.

Steps to Reproduce

//Get the absolute url of hub site
const webUrl = await getHubsiteUrl();

if(webUrl){
    const fileUrl = `${webUrl}/ConfigDocumenten/global.config.json`;
    const file = await fileFromAbsolutePath(sp.web, fileUrl); // throws error when fetching contextinfo (404 (Not found))
    const json: any = await file.getJSON();
    debugger;
}
patrick-rodgers commented 6 months ago

Are you 100% certain that file path is correct? Usually you can append contextinfo to whatever and it works. Is that sp.web pointing to the same site as the webUrl? Does this work if you run the code on the hubsite itself?

Perneel commented 6 months ago

Yes, the file path is correct. When I paste it in my browser, it starts downloading the file.

sp.web is the web of the site I'm testing on, the webUrl is the url of the hubsite. As a test, I created another Web for the hubsite but that throws the same error:

const _web = Web(webUrl).using(SPFx(context));
const fileUrl = `${webUrl}/ConfigDocumenten/global.config.json`;
const file = await fileFromAbsolutePath(_web, fileUrl);

I also tried to use the webpart in the workbench at the hubsite itself, so the same sitecollection as where the file is located. But that also results in the same error.

image image

patrick-rodgers commented 5 months ago

I am unable to duplicate this with any filename that doesn't end in .json so there must be something in the service that is treating those files differently.

I can fix this by not passing the absolute path to getContextInfo here, but the point of that was to get the context info from the target file and not the current object's context - which I think will break other stuff. So I am not sure that is the right change.

The challenge of parsing this, and why it was done this way (appending _/api/contextinfo) to the file path, is because there is not an efficient deterministic way to calculate from an absolute file path without an "_api" within what the appropriate web url should be.

This might be a case where this works for most cases (this is the first time this has come up in years) that we leave it and unfortunately, you'll need to craft a workaround in this specific case. Since you KNOW the containing web for the file is the hubsite you can do that fairly easily in ways we can't handle generically.

In fact, since you have the hubsite url and the absolute path of the file, it is likely much easier to simply do this:

import { spfi } from "@pnp/sp";

const hubsiteSP = spfi("hubsite web absolute url").using(...);
const file = await hubsiteSP.web.getFileByServerRelativePath("/sites/dev/Shared Documents/global.config.json")();
Perneel commented 5 months ago

@patrick-rodgers Good to know that it is related to json files in specific. Your suggestion with the relative path in the hubsite web does the trick indeed. Thanks for the input!

github-actions[bot] commented 5 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.