none
Type Error when using Sp.Core.js, sp.js, sp.runtime.js

    Question

  • I am trying to get sharepoint user name from Javascript but everytime I load the page I get a Error "type is undefined" with the 3 js files I need to use, then an error stating "SP is undefined"  I am assuming the SP error has to do with the type errors.

    Here is my code:

    <script type="text/javascript" src="/_layouts/SP.Core.js"></script>
    <script type="text/javascript" src="/_layouts/SP.js"></script>
    <script type="text/javascript" src="/_layouts/SP.Runtime.js"></script>
    <script type="text/javascript">
    
    function oninit(){
    var currentcontext = new SP.ClientContext.current();
    var currentweb = currentcontext.get_web();
    currentcontext.load(currentweb);
    var user = currentweb.get_currentuser();
    user.retrieve();
    currentcontext.load(currentweb);
    var login = user.get_loginName();
    document.getElementById("Name").value = login;
    }
    </script>
    

    Thanks for any and all help

     
    Wednesday, April 3, 2013 4:43 PM

Answers

  • ExecuteOrDelayUntilScriptLoaded() - This will resolve your issue.. Please try
    • Marked as answer by Billyj82 Wednesday, April 10, 2013 3:55 PM
    Thursday, April 4, 2013 4:46 AM
  • Hi Billyj82,

    Please use below code in your page.

    <script type="text/javascript" src="/_layouts/SP.Core.js"></script>
    <script type="text/javascript" src="/_layouts/SP.js"></script>
    <script type="text/javascript" src="/_layouts/SP.Runtime.js"></script>
    <script type="text/javascript">
    _spBodyOnLoadFunctionNames.push("oncustominit");
    function oncustominit(){
    var currentcontext = new SP.ClientContext.current();
    var currentweb = currentcontext.get_web();
    currentcontext.load(currentweb);
    var user = currentweb.get_currentuser();
    user.retrieve();
    currentcontext.load(currentweb);
    var login = user.get_loginName();
    document.getElementById("Name").value = login;
    }
    </script>

    Note: Don't call js method from Body onload directly because sharepoint hase some buildin methods so please use

    _spBodyOnLoadFunctionNames.push("oncustominit");

    Happy SharePointing...!!!


    Manoj | SharePoint

    • Marked as answer by Billyj82 Wednesday, April 10, 2013 3:55 PM
    Thursday, April 4, 2013 5:48 AM
  • I got the button working with this:

    <Sharepoint:ScriptLink runat="server" Name="SP.js" Localizable="false"  ID="s3" LoadAfterUI="true"/>
    <Sharepoint:ScriptLink runat="server" Name="SP.Core.js" Localizable="false"  ID="s1" LoadAfterUI="true"/>
    <Sharepoint:ScriptLink runat="server" Name="SP.Runtime.js" Localizable="false"  ID="s2" LoadAfterUI="true"/>
    <Sharepoint:ScriptLink runat="server" Name="SP.UI.Dialog.js" Localizable="false"  ID="s4" LoadAfterUI="true"/>
    <Sharepoint:ScriptLink runat="server" Name="CUI.js" Localizable="false"  ID="s5" LoadAfterUI="true"/>
    <Sharepoint:ScriptLink runat="server" Name="SP.UI.Rte.js" Localizable="false"  ID="s6" LoadAfterUI="true"/>
    <script type="text/javascript">
    
    var cc = null;
    var web = null;
    var user = null;
    var email = null;
    var title = null;
    var id = null;
    
    function oninit(){
    cc = new SP.ClientContext.get_current();
    web = cc.get_web();
    cc.load(web);
    user = web.get_currentUser();
    cc.load(user);
    cc.executeQueryAsync(null,null)
    var login = user.get_loginName();
    var email = user.get_email();
    var id = user.get_id();
    var title = user.get_title();
    alert("Login: " + login + " \r\nEmail: " + email + " \r\nID: " + id + " \r\nTitle: " + title);
    }
    ExecuteOrDelayUntilScriptLoaded(oninit,"SP.js");
    </script>
    


    Now I am trying to get it to pop up the alert "onload".  I am using the _spBodyOnLoadFunctionNames.push("oninit"); but everytime it loads it errors on 
    var login = user.get_loginName(); with this error:

    The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. 
    sp.runtime.js?rev=9sKdsC9N6p2BiRk3313M7Q%3D%3D, line 2 character 19964

    Any thoughts?

    • Marked as answer by Billyj82 Wednesday, April 10, 2013 3:56 PM
    Friday, April 5, 2013 2:49 PM

All replies

  • Check this post: http://social.technet.microsoft.com/forums/en-US/sharepointdevelopmentprevious/thread/265b65f2-e79e-4fe9-a764-1392ae97c569

    Most likely what's happneing is your function oninit() is being called before the SP.js script is loaded.  You can force it to fire after SP.js is loaded using the following:

    ExecuteOrDelayUntilScriptLoaded(oninit,"SP.js");
    

    Here is the MSDN on this function: http://msdn.microsoft.com/en-us/library/ff411788(v=office.14).aspx

    Here is a walkthrouhg on using the function: http://blog.allyis.com/delayed-loading-of-custom-javascript-in-sharepoint/


    Brandon Atkinson
    Blog: http://brandonatkinson.blogspot.com

    Wednesday, April 3, 2013 6:06 PM
  • I am still getting the same errors with this one too:

    The value of the property 'ExecuteOrDelayUntilScriptLoaded' is null or undefined, not a Function object

    <script type="text/javascript" src="/_layouts/SP.Core.js"></script>
    <script type="text/javascript" src="/_layouts/SP.js"></script>
    <script type="text/javascript" src="/_layouts/SP.Runtime.js"></script>
    <script type="text/javascript">
    
    function oninit(){
    var currentcontext = new SP.ClientContext.current();
    var currentweb = currentcontext.get_web();
    currentcontext.load(currentweb);
    var user = currentweb.get_currentuser();
    user.retrieve();
    currentcontext.load(currentweb);
    var login = user.get_loginName();
    document.getElementById("Name").value = login;
    }
    ExecuteOrDelayUntilScriptLoaded(oninit,"SP.js");
    </script>
    </head>
    
    <body onload="oninit()">
    
    <form id="form1" runat="server">
    <asp:Label ID="Name" runat="server"></asp:Label>
    </form>
    
    </body>
    
    </html>
    When I run the debug on the type error its at this location on all 3 .js files
    Type.registerNamespace("SP")


    • Edited by Billyj82 Wednesday, April 3, 2013 9:07 PM updated
    Wednesday, April 3, 2013 8:58 PM
  • Where's your currentContext.executequeryasync() function;  omitted for clarity ?

    Kind Regards

    Bjoern
    http://www.sharepointviking.com
    Twitter: Follow @bjoern_rapp

    Wednesday, April 3, 2013 9:21 PM
    Moderator
  • ExecuteOrDelayUntilScriptLoaded() - This will resolve your issue.. Please try
    • Marked as answer by Billyj82 Wednesday, April 10, 2013 3:55 PM
    Thursday, April 4, 2013 4:46 AM
  • Hi Billyj82,

    Please use below code in your page.

    <script type="text/javascript" src="/_layouts/SP.Core.js"></script>
    <script type="text/javascript" src="/_layouts/SP.js"></script>
    <script type="text/javascript" src="/_layouts/SP.Runtime.js"></script>
    <script type="text/javascript">
    _spBodyOnLoadFunctionNames.push("oncustominit");
    function oncustominit(){
    var currentcontext = new SP.ClientContext.current();
    var currentweb = currentcontext.get_web();
    currentcontext.load(currentweb);
    var user = currentweb.get_currentuser();
    user.retrieve();
    currentcontext.load(currentweb);
    var login = user.get_loginName();
    document.getElementById("Name").value = login;
    }
    </script>

    Note: Don't call js method from Body onload directly because sharepoint hase some buildin methods so please use

    _spBodyOnLoadFunctionNames.push("oncustominit");

    Happy SharePointing...!!!


    Manoj | SharePoint

    • Marked as answer by Billyj82 Wednesday, April 10, 2013 3:55 PM
    Thursday, April 4, 2013 5:48 AM
  • Manoj I took your advice and added a button onclick event that just has an alert at the end.  That seemed to solve all the type problems.  In the long run I will need the onload event to work but I can research that more.  Your "_spBodyOnLoadFunctionNames.push("oninit")" did not seem to work.  But for this thread I just want to get the user info then I can research what I'm doing wrong with the onload.  

    Now whenever it hits the "New SP.ClientContext.current();" It says SP.ClientContext is undefined.  Do I need to have a master page attached for this to work?

    Friday, April 5, 2013 12:25 PM
  • I got the button working with this:

    <Sharepoint:ScriptLink runat="server" Name="SP.js" Localizable="false"  ID="s3" LoadAfterUI="true"/>
    <Sharepoint:ScriptLink runat="server" Name="SP.Core.js" Localizable="false"  ID="s1" LoadAfterUI="true"/>
    <Sharepoint:ScriptLink runat="server" Name="SP.Runtime.js" Localizable="false"  ID="s2" LoadAfterUI="true"/>
    <Sharepoint:ScriptLink runat="server" Name="SP.UI.Dialog.js" Localizable="false"  ID="s4" LoadAfterUI="true"/>
    <Sharepoint:ScriptLink runat="server" Name="CUI.js" Localizable="false"  ID="s5" LoadAfterUI="true"/>
    <Sharepoint:ScriptLink runat="server" Name="SP.UI.Rte.js" Localizable="false"  ID="s6" LoadAfterUI="true"/>
    <script type="text/javascript">
    
    var cc = null;
    var web = null;
    var user = null;
    var email = null;
    var title = null;
    var id = null;
    
    function oninit(){
    cc = new SP.ClientContext.get_current();
    web = cc.get_web();
    cc.load(web);
    user = web.get_currentUser();
    cc.load(user);
    cc.executeQueryAsync(null,null)
    var login = user.get_loginName();
    var email = user.get_email();
    var id = user.get_id();
    var title = user.get_title();
    alert("Login: " + login + " \r\nEmail: " + email + " \r\nID: " + id + " \r\nTitle: " + title);
    }
    ExecuteOrDelayUntilScriptLoaded(oninit,"SP.js");
    </script>
    


    Now I am trying to get it to pop up the alert "onload".  I am using the _spBodyOnLoadFunctionNames.push("oninit"); but everytime it loads it errors on 
    var login = user.get_loginName(); with this error:

    The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. 
    sp.runtime.js?rev=9sKdsC9N6p2BiRk3313M7Q%3D%3D, line 2 character 19964

    Any thoughts?

    • Marked as answer by Billyj82 Wednesday, April 10, 2013 3:56 PM
    Friday, April 5, 2013 2:49 PM
  • Change the line "cc.load(user)" to

    cc.load(user,'LoginName');

    This will load the LoginName property of the client object. Not all properties of client objects are available after query execution. We have to explicitly request for those property values.

    Reference: http://msdn.microsoft.com/en-us/library/ee539350(v=office.14).aspx


    Pradip T. ------------- MCTS(SharePoint 2010/Web)|MCPD(Web Development) https://www.mcpvirtualbusinesscard.com/VBCServer/paddytakate/profile

    Friday, April 5, 2013 3:00 PM
  • It still works fine with the button by gives me an error

    The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. 
    sp.runtime.js?rev=9sKdsC9N6p2BiRk3313M7Q%3D%3D, line 2 character 19964

     on page load.  On the line

    var login = user.get_loginName();

     

    Friday, April 5, 2013 5:03 PM