"Uncaught ReferenceError: SP is not defined" when trying to access in JS code in aspx file

Dariusz Biskupski 20 Reputation points
2023-04-26T10:26:49.9533333+00:00

Hi All I have built a site in HTML, CSS & JavaScript. In JS file I want to access user name with the following code:

var currentContext = SP.ClientContext.get_current();
var currentUser = currentContext.get_web().get_currentUser();
$('.user-name').text(currentUser);

I normally rename html site into aspx and upload to Shartepoint Pages. When I open this aspx site now it shows the above error. However I can acess SP object when I am in Sharepoint Pages. That makes me think I need to pull a library from somewhere to have it working. I already tried the following but nothing works. Please help

   <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
    <script type="text/javascript" src="/_layouts/15/sp.js"></script>
    <script type="text/javascript" src="../Scripts/App.js"></script>
    <script type="text/javascript" src="../Scripts/Add-in.js"></script>


SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,300 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 33,641 Reputation points Microsoft Vendor
    2023-04-27T02:15:52.0966667+00:00

    Hi @Dariusz Biskupski

    You will need to check if you have the access to the library where you store the js file of JSOM. To get a current user by JSOM you can refer to following code

    <script type="text/javascript">  
    ExecuteOrDelayUntilScriptLoaded(init,'sp.js');  
    var currentUser;  
    function init(){  
        this.clientContext = new SP.ClientContext.get_current();  
        this.oWeb = clientContext.get_web();  
        currentUser = this.oWeb.get_currentUser();  
        this.clientContext.load(currentUser);  
        this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));  
    }  
      
    function onQuerySucceeded() {  
        document.getElementById('userLoginName').innerHTML = currentUser.get_loginName();   
        document.getElementById('userId').innerHTML = currentUser.get_id();  
        document.getElementById('userTitle').innerHTML = currentUser.get_title();  
        document.getElementById('userEmail').innerHTML = currentUser.get_email();  
    }  
      
    function onQueryFailed(sender, args) {  
        alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());  
    }  
    </script>  
    <div>Current Logged User:  
        <span id="userLoginName"></span></br>  
        <span id="userId"></span></br>  
        <span id="userTitle"></span></br>  
        <span id="userEmail"></span></br>  
    </div> 
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.