none
Javascript: Unable to get property 'get_current' of undefined or null reference RRS feed

  • Question

  • Hi everyone :)

    I'm doing an application on javascript with project server 2013, Here the code where I have my main trouble:

        <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
        <script language="javascript" type="text/javascript">  
            $(document).ready(function() {  
                SP.SOD.executeOrDelayUntilScriptLoaded(handleCreateSite, "sp.js");  
            });  
    
    function handleCreateSite() {
    // Declare global variables.
    var Temp_PCE;             // Contains the Project Center extension object.
    var projContext;
    var projects;
    
    
        // Initialize the current client context.
        projContext = PS.ProjectContext.get_current();
    
        // Get the projects collection.
        projects = projContext.get_projects();
    
        // Register the request for information that you want to run on the server.
        projContext.load(projects);
    
        // Run the request on the server.
        projContext.executeQueryAsync(IterateThroughProjects, QueryFailed);
    }
    
    
    </script>

    But when I click on the button in my ribbon, I have the following error message:

    Unable to get property 'get_current' of undefined or null reference

    So after some searches, it could come because the Sp wasn't load, so I added this line :

    SP.SOD.executeOrDelayUntilScriptLoaded(handleCreateSite, "sp.js");

    But nothing has changed, I'm still struggle with this issue

    Any idea of what I'm doing wrong?


    • Edited by freedumz Wednesday, February 27, 2019 6:18 AM
    Wednesday, February 27, 2019 6:18 AM

All replies

  • Hi,

    Check if following scripts references have loaded in browser dev tools before your script gets executed, else add the references and delay execution until PS.js is loaded

    <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="/_layouts/15/ps.js"></script>

    <script type="text/javascript">

    SP.SOD.executeOrDelayUntilScriptLoaded(handleCreateSite, "PS.js");

    </script>

    Wednesday, February 27, 2019 8:23 AM
  • Hi ,

    Below is the piece of code that retrieves projects information.

    <script src="https://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <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="../_layouts/15/ps.js"></script>

    <script type="text/javascript">

    $(document).ready(function(){

      SP.SOD.executeOrDelayUntilScriptLoaded(ReadProjects,"PS.js");
     
    function ReadProjects(){
       
            var projectContext = PS.ProjectContext.get_current();
            var allProjects=projectContext.get_projects();       
            projectContext.load(allProjects);            
            projectContext.executeQueryAsync(GetProjectData, errorHandler);
                   
            function GetProjectData() {           
                var projectEnumerator = allProjects.getEnumerator();
                while (projectEnumerator.moveNext()) {             
                    var projectInfo = projectEnumerator.get_current();
     
    console.log(projectInfo.get_name());

      }                                                                      
                }
         }

    });

    </script>

    Hope it helps you.

    Wednesday, February 27, 2019 9:17 AM
  • Hi Freedumz,

    As mentioned by Ashok you will need add the scripts in your webpart to make sure the required libraries loaded before your scripts communicates to Project Online/Server.

    <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="/_layouts/15/ps.js"></script>

    Detailed informatiom about the reading project data using jsom is listed in this link  https://sharepointprojectserver.com/read-project-online-project-data-using-jsom/ 

    Saturday, March 2, 2019 12:27 PM