Today we faced with interesting problem when tried to use sp.site.exists(url) method from PnP JS for determining whether site collection with specified url exists or not: all calls returned 403 with System.UnauthorizedAccessException:
During analysis I've found that it tried to make HTTP POST calls to the following url for all sites: https://{tenant}.sharepoint.com/sites/_api/contextinfo which is definitely not correct because https://{tenant}.sharepoint.com/sites is not correct url but managed path. Then we noticed that page where this code was executed contains #/ in url:
https://{tenant}.sharepoint.com/sites/mysitecol#/favorites
and it seems like PnP JS parses it incorrectly in this case. As workaround we checked which API is used inside sp.site.exists(url) call (/_api/SP.Site.Exists) and rewrote code with explicit API call instead:
let promise = context.spHttpClient.post(context.pageContext.site.absoluteUrl + "/_api/SP.Site.Exists", SPHttpClient.configurations.v1, { headers: { "accept": "application/json;" }, body: JSON.stringify({ url: url }) }).then((response: SPHttpClientResponse) => { response.json().then((exists: any) => { if (exists.value) { // site exists } else { // site doesn't exist } }); });
Hope that PnP JS will fix this issue at some point.
Update 2021-07-05: issue inside pnp js was fixed by adding initialization call:
sp.setup({ spfxContext: this.context });
See this discussion for more details.
No comments:
Post a Comment