I have a sharepoint app with multiple .aspx pages. On different pages I push and pull from sharepoint lists and I would like to send emails. These require me knowing hostUrl
and appWebUrl
.
I would like to get hostUrl and appWebUrl using the following code, which I have found on multiple tutorials:
var appWebUrl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
var hostUrl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
However, those only work the very first time the page is loaded, and only work on Default.apsx.
>> getQueryStringParameter('SPAppWebUrl');
"https%3A%2F%2FmyCompany4%LETTERSANDNUMBERS%2Esharepoint%2Ecom%2Fmy%2Fpath%2FmyApp"
>> getQueryStringParameter('SPHostUrl');
"https%3A%2F%2Fmycompany4%2Esharepoint%2Ecom%2Fmy%2Fpath"
If I navigate from Default.apsx to Page2.apsx, or simply load to Page2.aspx initially, those lines return the following error:
>> getQueryStringParameter('SPAppWebUrl');
Uncaught TypeError: Cannot read property 'split' of undefined
>> getQueryStringParameter('SPHostUrl');
Uncaught TypeError: Cannot read property 'split' of undefined
This is because getQueryStringParameter
(seen here) uses document.URL
which changes significantly after navigating to a different page.
>> console.log(document.URL) // before
https://mycompany-123456789.sharepoint.com/my/path/MyApp…123456%2Esharepoint%2Ecom%2Fmy%2Fsite%2FmyApp
>> console.log(document.URL) // after
https://mycompany-123456789.sharepoint.com/my/path/myApp/Pages/Page2.aspx
For hostUrl
I think I could do:
>> "mycompany.sharepoint.com" + SP.ClientContext.get_current().get_url();
https://mycompany.sharepoint.com/my/path/myApp
But that doesn't work for appWebUrl
because I need the number/letter code in the url to send emails.
https://mycompany-12345ABCDE.sharepoint.com/my/path/myApp
Why doesn't getQueryStringParameter work on different pages and what's another method of retrieving these urls that would work accross different pages?
Thanks in advance.