Search code examples
sharepointoffice-jssharepoint-jsom

Office Addin _spPageContextInfo Undefined


I am getting _spPageContextInfo Undefined on office Addins

i added the below sharepoint js files into my project but still getting the same error _spPageContextInfo Undefined,

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

$(document).ready(function () {
   ExecuteOrDelayUntilScriptLoaded(runCode, "sp.js");

});


function runCode() {
            var userid = _spPageContextInfo.userId;
}

Most examples use sharepoint hosted addin but i would like to used Office Addin.

Is there any js file i should add ? I couldn't use the guide because it only work for documents created with the addin content type https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/create-a-sharepoint-add-in-that-contains-a-document-template-and-a-task-pane-add


Solution

  • The _spPageContextInfo is only injected by Sharepoint when you are on a sharepoint classic page. Even for Sharepoint framework components they don't recommend to use it because it might not be on the page.