I'm building a SPA inside Sharepoint 2013 by uploading javascript and html files to a document library. I'm trying to get current user info by accessing context info through the sp.js library. But i get this error:
_spPageContextInfo is not defined
In my index.aspx file i include the following js:
<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>
I also included this at the top of the index.aspx file:
<%@ Page language="C#" %>
<%@ Register Tagprefix="SharePoint"
Namespace="Microsoft.SharePoint.WebControls"
Assembly="Microsoft.SharePoint, Version=14.0.0.0,
Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
In my js file i have the following code:
$(document).ready(function() {
// wait for the sharepoint javascript libraries to load, then call the function 'Initialize'
ExecuteOrDelayUntilScriptLoaded(runCode, "sp.js");
});
function runCode() {
var userid= _spPageContextInfo.userId;
}
It should be mentioned that i use Angular JS to build the app, and I use Angular UI Router library for navigation between pages.
The document library is inside a subsite in a side collection.
E.g.
intra.xxx.xxx/xxx/index.aspx
Any suggestions?
It turned out that I apperantly was missing some sharepoint dependencies on my index.aspx file.
Solution
Start of index.aspx:
<%@ Page language="C#" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<!DOCTYPE html>
Scripts in header:
<!--Sharepoint Dependencies-->
<script src="/_layouts/1033/init.js"></script>
<script src="/_layouts/1033/core.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>
<script src="/_layouts/SP.UI.Dialog.js"></script>
<script src="/_layouts/ScriptResx.ashx?culture=en%2Dus&name=SP%2ERes"></script>
Body:
<!-- required: SharePoint FormDigest -->
<form runat="server">
<SharePoint:FormDigest runat="server"></SharePoint:FormDigest>
</form>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With