Forum Discussion
Mike Jansen
Jun 14, 2017Iron Contributor
Sharepoint framework spfx > get weburl
"In the old days" I would use something like: var url = _spPageContextInfo.webServerRelativeUrl; To get the URL of my SharePoint site. Now I try to do the same thing in SPFX like: this.context.pa...
Jun 14, 2017
Hi Mike Jansen,
this.context.pageContext.web.absoluteUrl
is the right way ,but document.ready is not the right way ;-)
you can simply call whatever you need within the render method.
You will have to use methods with a promise like this:
public _getListDataUsers(): Promise<Users> { return this.context.spHttpClient.get(this.context.pageContext.web.absoluteUrl + `/_api/web/siteusers`, SPHttpClient.configurations.v1) .then((response: SPHttpClientResponse) => { return response.json(); }); }
The above example will work within a render method as the context will be alive.
If you for example run the method from a document ready then you will get some errors as you are experiencing.
You will need roughly something like this if you want an onclick event:
var myElement: Element = document.querySelector(`#myElement`); myElement.addEventListener('click', (evt: Event) => { this._getListDataUsers }, false);
Mike Jansen
Jun 15, 2017Iron Contributor
hi Pieter Veenstra.
Thanks again, I will dive into this.