11

I have created a client web part for a SharePoint-hosted app that I am creating. The app makes use of the clienttemplates.js library in SharePoint to render user presence (Lync) in the web page.

I am getting an error that "_spPageContextInfo is undefined" in clienttemplates.js. From doing a little research, it appears that _spPageContextInfo is defined in _layouts/15/core.js, which I am including in my client web part head section:

<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<script type="text/javascript" src="../Scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script type="text/javascript" src="/_layouts/15/init.js"></script>  
<script type="text/javascript" src="/_layouts/15/core.js"></script>
<script type="text/javascript" src="/_layouts/15/1033/strings.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>

Looking at the network tab of developer tools indicates that the core.js file is being loaded. What else am I missing?

4
  • 1
    client web parts are loaded in iframes, which don't instantiate _spPageContextInfo, they do however get fed all the info from whatever URL tokens you pass. What information do you need from the _spPageContextInfo object?
    – John-M
    Commented Aug 11, 2014 at 1:28
  • One of the methods I am calling in clienttemplates.js is trying to access _spPageContextInfo.crossDomainPhotosEnabled. My code doesn't need that information, but the method I'm calling into does. Is there anything I can do to fake an instance of _spPageContextInfo to make the clienttemplates.js code happy?
    – JasonV
    Commented Aug 11, 2014 at 19:27
  • 3
    Well nothing prevents you from writing 'window._spPageContextInfo = { crossDomainContextPhotosEnabled: true };' but I have no idea how it would affect any of the other code :)
    – John-M
    Commented Aug 12, 2014 at 2:26
  • Well, that worked. Now, how long do I run this in test before I decide it's trustworthy. In any case, thanks for the help!
    – JasonV
    Commented Aug 12, 2014 at 15:11

3 Answers 3

9

you need to add the following control to your page body:

 <SharePoint:SharePointForm runat="server" >
  </SharePoint:SharePointForm>
4

IF...

 <SharePoint:SharePointForm runat="server" ></SharePoint:SharePointForm>

gives you an error try:

<form id="Main" name="Main" runat="server"></form>

add the tag to the body or header before loading

_spPageContextInfo

lastly, try using this script call stack

<SharePoint:ScriptLink Name="MicrosoftAjax.js" runat="server" Defer="False" Localizable="false"/>
<SharePoint:ScriptLink Name="SP.core.js" runat="server" Defer="False" Localizable="false"/>
<SharePoint:ScriptLink Name="SP.js" runat="server" Defer="False" Localizable="false"/>
<SharePoint:ScriptLink Name="SP.runtime.js" runat="server" Defer="False" Localizable="false"/>
1

A possible solution is to make a copy of Default.aspx and use that page in your App Part. You'll have to strip down the branding of SharePoint using some simple CSS to display properly, but somehow the rendering of this ASPX page results in _spPageContextInfo object to be available. Try it with Default.aspx. In your console you can easily test for _spPageContextInfo.

Your Answer

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

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