pnp / pnpjs

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

Get a SharePoint List setup on root web site #3099

Closed addin-dev-24 closed 2 months ago

addin-dev-24 commented 2 months ago

What version of PnPjs library you are using

3.x

Minor Version Number

20

Target environment

SharePoint Framework

Additional environment details

I am using a SharePoint web part extention.

Question/Request

I am developing a SharePoint web part extension. I am trying to get a List from my root web site. I use the following code,


import "@pnp/sp/sites";

import { SPFI, SPFx, spfi } from '@pnp/sp';

 private sp: SPFI;

 const rootWeb = await this.sp.site.getRootWeb();

  const rootwebData = await this.sp.site.rootWeb();

Both rootWeb and rootwebData return details about current web site. Not the details about root web site. What cloud be the issue here

bcameron1231 commented 2 months ago

Hi, for clarity, a Root Web is the highest level Web of a Site Collection, not the Root of your SharePoint tenant.

You're going to want to use the Web Factory method as outlined by our docs here. https://pnp.github.io/pnpjs/sp/webs/

const root = Web("https://tenant.sharepoint.com").using(SPFx(this.context));

addin-dev-24 commented 2 months ago

@bcameron1231 Thank you for the information. It was very helpful. As you mentioned, I was under the impression that rootWeb() returns data about the root of the SharePoint tenant. I did some research and found the code below to get the root of the tenant:

import "@pnp/sp/sites";

import { SPFI, SPFx, spfi } from '@pnp/sp';

private _rootSp: SPFI; 

const currentSiteUrl = this.context.pageContext.web.absoluteUrl;
const tenantRootUrl = new URL(currentSiteUrl).origin;

this._rootSp = spfi(tenantRootUrl).using(SPFx(this.context));

const rootWeb = await this._rootSp.site.getRootWeb();

const currentSiteUrl = this.context.pageContext.web.absoluteUrl; const tenantRootUrl = new URL(currentSiteUrl).origin;

These code lines will get the current web site URL and construct the tenanu URL by using the Originof current URL. This code will not work if tenant root and current web site have different domains(custom domain)

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