0

The _spPageContextInfo.siteServerRelativeUrl is undefined which i'm using to acquire information about the web parts on a page. unfortunately, the value always returns undefined. I tried using the following.

SP.SOD.executeFunc('SP.js','SP.ClientContext',this.function); 

When that is executed I get a undefined error for the spPageContextInfo.

SP.SOD.executeFunc('SP.js','SP.PageContextInfo',this.function);

When I run the statement with SP.PageContextInfo then the function is never executed. This is all done using typescript and React on Render. I feel as if this is a simple issue but seem to have exhausted most resources.

How to initialize the _spPageContextInfo without a race condition for a web part that is run on SharePoint online?

1

We need add the code below into the page.

 <SharePoint:FormDigest ID="FormDigest1" runat="server"></SharePoint:FormDigest>

Full code of aspx page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ Page Language="C#" %>
<%@ Register tagprefix="SharePoint" namespace="Microsoft.SharePoint.WebControls" assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml" xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">

<head runat="server">
<meta name="WebPartPageExpansion" content="full" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<meta http-equiv="X-UA-Compatible" content="IE=10" />
<SharePoint:CssRegistration Name="default" runat="server"/>

<!--[if gte mso 9]>
<SharePoint:CTFieldRefs runat=server Prefix="mso:" FieldList="FileLeafRef,WikiField,_dlc_DocId,_dlc_DocIdUrl,_dlc_DocIdPersistId"><xml>

<mso:CustomDocumentProperties>
<mso:_dlc_DocId msdt:dt="string">5JSTYM4NXMKN-1550736763-35</mso:_dlc_DocId>
<mso:_dlc_DocIdItemGuid msdt:dt="string">11042d2a-78f3-4163-b78b-a7e5c9bdbf00</mso:_dlc_DocIdItemGuid>
<mso:_dlc_DocIdUrl msdt:dt="string">http://sp2013/sites/team/_layouts/15/DocIdRedir.aspx?ID=5JSTYM4NXMKN-1550736763-35, 5JSTYM4NXMKN-1550736763-35</mso:_dlc_DocIdUrl>
</mso:CustomDocumentProperties>
</xml></SharePoint:CTFieldRefs><![endif]-->

<script type="text/javascript" src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(function () {
    alert(_spPageContextInfo.siteServerRelativeUrl);
});
</script>
</head>

<body>
<form id="form1" runat="server">
<SharePoint:FormDigest ID="FormDigest1" runat="server"></SharePoint:FormDigest>
</form>
</body>
</html>

enter image description here

If you want to use JSOM code, we need load some JavaScript files:

<script src="/_layouts/1033/init.js"></script>
<script src="/_layouts/MicrosoftAjax.js"></script>
<script src="/_layouts/sp.core.js"></script>
<script src="/_layouts/sp.runtime.js"></script>
<script src="/_layouts/sp.js"></script>

More information: Bare minimum page to get _spPageContextInfo loaded

0

I ran into a similar issue, but it was caught during build as I am using CreateReactApp, and they have a check for no undefined variables. Their suggestion (and it worked for me) is that you redefine _spPageContextInfo like so:

const _spPageContextInfo = window._spPageContextInfo;

That's it, then just use _spPageContextInfo as you normally would.

FWIW I put that line of code after my imports, but before any of the component code, but I suppose it would work from within the render function as well.

  • I gave the above code snippet a whirl. Unfortunately, window._spPageContextInfo is returning a typescript error that states _spPageContextInfo doesn't exist on type window – scoutIn Nov 9 '17 at 15:17
0

just load the scripts first before you start using the JS code block:

<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>

<script>

SP.SOD.executeFunc('SP.js','SP.ClientContext',this.function); 


</script>

edit

sorry missed the spfx part!

import { IWebPartContext} from '@microsoft/sp-webpart-base';

And then you can do (*assume the context has the stuff you are looking for in):

public context: IWebPartContext;

And then, for example:

url = this.context.pageContext.web.absoluteUrl + "/_api/web/lists/GetByTitle('SomeList')/

PageContext in SharePoint Framework

in spfx you are simply replacing:

_spPageContextInfo.siteServerRelativeUrl  

with

this.context.pageContext.web.absoluteUrl
  • Since i'm using react and typescript I have a manifest.json that loads all the scripts that I use before ever executing the script code. – scoutIn Nov 9 '17 at 15:32
  • sorry missed that part lol. is this in debug? if so this should do it.... import { IWebPartContext} from '@microsoft/sp-webpart-base'; – Ali Jafer Nov 9 '17 at 16:24
  • the you can.... public context: IWebPartContext; and use the context. taken from here: sharepoint.stackexchange.com/questions/198652/… – Ali Jafer Nov 9 '17 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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