8

I am creating a simple example that demonstrates client object model using javascript. I have ceated a simple JS file in Visual Studio and an aspx page using the SharePoint Designer.

I have used the following command in my aspx page to include the sp.js and sp.runtime.js files but no success and so I get an error which says "ReferenceError: Type is not defined" when I run the page. the command that is used to include the sp.js is:

<script type="text/javascript" src="/_layouts/sp.js"></script>

and the code in my js file which I created using Visual Studio is:

jQuery(document).ready(function ()
{
 retrieveListItems();

}); 

I am working on SharePoint 2013 enterprise edition and I have not used custom master page.

I am not able to figure out what is issue. Comment ASAP.

7
  • Please confirm if this is SharePoint online? Are you using a custom master page? The sp.js reference you added is it inside the AdditionalPageHead placeholder? Commented Dec 18, 2014 at 12:58
  • Ok how about the 3rd question. Commented Dec 18, 2014 at 13:00
  • Please try that. Commented Dec 18, 2014 at 13:02
  • Let us continue this discussion in chat. Commented Dec 18, 2014 at 13:03
  • For some reason Chat page is not allowing me to login.. Ok can you please open the aspx page in SharePoint designer.. then search for AdditionalPageHead.. Commented Dec 18, 2014 at 13:05

8 Answers 8

12

Try adding following references to the additional page head of the page

<script type="text/javascript" src="_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="_layouts/15/sp.js"></script>

Then try using following script

jQuery(document).ready(function ()
{
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', retrieveListItems);
}); 

function retrieveListItems() {
    alert('Libraries Loaded');
}
2
  • Can you create a page using SharePoint UI instead of Designer? Commented Dec 18, 2014 at 13:14
  • In my case it worked just using jQuery(document).ready(function() { SP.SOD.executeFunc(…) }); because Sharepoint was loading sp.js after my script
    – Piero
    Commented Oct 3, 2018 at 15:20
5

I used include all the requires javascripts as per the below sequence and there by I was able to include the SP.js.

<script src="/_layouts/1033/init.js" type="text/javascript"></script>
<script src="/_layouts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/_layouts/sp.core.js" type="text/javascript"></script>
<script src="/_layouts/sp.runtime.js" type="text/javascript"></script>
<script src="/_layouts/sp.js" type="text/javascript"></script>

I have referred this link.

The most important of all is that one should include jquery.min.js and not jquery.js. This is small point which would create havoc in your life.

1
  • So was your problem in the end that your reference to jquery was incorrect?
    – Submits
    Commented Dec 19, 2014 at 17:58
4

It could be you're calling retrieveListItems before SP.js has been fully loaded, in which case try:

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', retrieveListItems);
3
  • I have tried this but it gives Type not defined error for SP.js. Commented Dec 18, 2014 at 12:22
  • Are you sure sp.js is actually in the _layouts folder? Try deploying it to a different location, like SiteAssets
    – Submits
    Commented Dec 18, 2014 at 12:30
  • Yes, I have checked that. Commented Dec 18, 2014 at 12:33
3

Add a reference to "/_layouts/MicrosoftAjax.js" and you no longer get the Type is not defined error. I got the answer here.

6
  • Sorry to say but even this does not work. Commented Dec 18, 2014 at 12:43
  • Try this... <script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
    – Avijit Sur
    Commented Dec 18, 2014 at 12:48
  • It is the ditto that I have used. Do you have some other solutions for this mind boggling problem? Commented Dec 18, 2014 at 12:50
  • Can you please do something like below using ExecuteOrDelayUntilScriptLoaded()... var clientContext; ExecuteOrDelayUntilScriptLoaded(initialize,"SP.js"); function initialize() { this.clientContext = SP.ClientContext.get_current(); }
    – Avijit Sur
    Commented Dec 18, 2014 at 13:06
  • Unless the SP.js file is loaded none of the code you mentioned will execute. Commented Dec 18, 2014 at 13:12
2

Since you have jQuery loaded, perhaps you can use the $.getScript() function to load SP.js:

var scriptbase = siteUrl + '/_layouts/15/';

$.getScript(scriptbase + 'SP.Runtime.js',
    function() {
        $.getScript(scriptbase + 'SP.js', retrieveListItems);
    }
);

Info on $.getScript() here.

2
  • What does _spPageContextInfo stand for? I am getting an error in this. Commented Dec 19, 2014 at 5:35
  • It contains general info about the page. You can just hard code the site url in place of that. I'll edit my answer.
    – wjervis
    Commented Dec 19, 2014 at 11:02
0

always must use

<SharePoint:ScriptLink language="javascript" name="sp.js" OnDemand="true" runat="server" Localizable="false" />

I lost 2 days fighting with this , i dont know why nut it doesnt works using script src

0

I had the exactly the same problem because i referred the wrong List Column Internal Name in oListItem.get_item('ListFieldName')

-1

I said this before in another question:

Add javascript to sharepoint 2013

you can reference scripts in the masterpage of this forma:

<SharePoint:ScriptLink language="javascript" name="custom.js" OnDemand="true" runat="server" Localizable="false" />

Referncing a java script inside my master page will not fire

NOTE: you can use <SharePoint:ScriptLink language="javascript" name="custom.js" OnDemand="true" runat="server" Localizable="false" /> in a aspx file. For using it with JS SP.SOD.executeFunc('sp.js', 'SP.ClientContext', retrieveListItems);

4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.