5

I have a script that provision site collections under a specific managed path. In these site collections, I have sub webs with page libraries in which I have several different pages with custom javascript code that requires some properties that can be accessed through the _spPageContextInfo object.

I have two functions that both need to get the current pageItemId and it's corresponding pageListId. My problem is that after the page has loaded, none of these two properties are available through the _spPageContextInfo object.

The strange thing is that I can access them if I pause the loading of the page during page load through a javascript breakpoint on $(document).ready(), and at that point both properties are available.

Another thing I noticed is that when I've paused the execution, the _spPageContextInfo.siteServerRelativeUrl is set to "/myManaged/Path/mySubweb" while when the page is loaded, it is set to "/". The same applies to webServerRelativeUrl,webAbsoluteUrl and siteAbsoluteUrl, though they revert back to the root instead of the current site collection and web.

How come that the properties of _spPageContextInfo changes from these two states, and why aren't pageItemId and pageListId available when the page has finished loading? Does this have something to do with the fact that they are created under a managed path?

1 Answer 1

2

It turns out the answer was right in front of me all the time. When the execution of the script paused on the breakpoint, the _spPageContextInfo was correct, as stated in the question. Stepping forward in the code, I tried to spot at what moment the properties of the object changed, and it all became quite obvious.

One of the functions looked something like this

$('#element').load("https://pathToAnotherSite"), function() { ... }

When the above code got executed, the current _spPageContextInfochanged to the context of the above site path instead of the current one. I'm not sure if it is possible and if so how you can change the current _spPageContextInfo (I tried making a dummy $.load() to my current page path) so my solution is to save away the _spPageContextInfo in another variable while it's accessible.

Comments or solutions on how to switch context would be very nice.

2
  • 1
    I think _spPageContextInfo is rendered in the HTML (a script tag) by SharePoint, so when you do a .load, and get an new page. It will overwrite the current _spPageContextInfo variable Jun 10, 2015 at 12:08
  • That sounds reasonable. I changed my solution and reassigned the _spPageContextInfo after the load, because I noticed that some OOB features stopped working, since they most likely relies on it to be linked to the current page.
    – Daniel B
    Jun 10, 2015 at 13:04

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.