0

I have a SharePoint SPFx react webapp that I'm working on. I'm trying to get the SharePoint URL. For example:

https://mysite.sharepoint.com/sites/sitename/SitePages/Home.aspx#/ 

So I want to get https://mysite.sharepoint.com/sites/sitename

I've tried using references from here: Get Site Collection full URL using javascript and spfx-how-use-site-url-from-current-page-context-in-react-spfx-solution

The issue I'm having is I'm using the older sp-pnp-js library so all the examples I found online reference the newer pnpJS library.

So this.context.pageContext.web.absoluteUrl and spHttpClient do not work with the older sp-pnp-js library

1 Answer 1

0

You can use this.context.pageContext.web.absoluteUrl with the older sp-pnp-js library.

I have used something similar to below and it works in our tenant.

import { sp, Web } from 'sp-pnp-js';
const web = new Web(this.context.pageContext.web.absoluteUrl);
web.lists.getByTitle('list name').items.getById(1250).select('Title').get();
3
  • When I used that, I get a 'context' is deprecated error and the page fails. did this in my componentDidMount and then console.log(web)
    – hisusu32
    Oct 13, 2021 at 22:06
  • How are you passing the content object to the component being rendered, i do something similar to this. const element: React.ReactElement<IFormProps > = React.createElement( Form, { context: this.context } )
    – ayodele
    Oct 14, 2021 at 10:47
  • Also if you are using the old interface IWebPartContext that would be why you are getting that error as it is deprecated. There is a newer interface WebPartContext. In my case my webpart props looks like this export interface IFormWebPartProps { description: string; listName: string; context: WebPartContext; }
    – ayodele
    Oct 14, 2021 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.