6

I keep receiving the error message "SP.ClientContext is not a Constructor" every time I try to use it in my JS code. After doing a bit of research online, I found SP.SOD.execute function(). However, I can't figure out if I am using it wrong, or if there may be another problem with my code. I created a simple .ASPX page in SharePoint to test this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ Page language="C#" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=XXX" %>
<SharePoint:ScriptLink Name="SP.core.js" runat="server" Defer="False" Localizable="false"/>
<SharePoint:ScriptLink Name="SP.js" runat="server" Defer="True" Localizable="false"/>

<html xmlns="http://www.w3.org/1999/xhtml">

<script>

var siteUrl = [MY URL];

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

function retrieveListItems() {
try{
    var clientContext = new SP.ClientContext(siteUrl);

   ...

}catch(err){
    alert(err.message);

}
</script>
<head>
<meta name="WebPartPageExpansion" content="full" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


<input type="button" value="Click Me!" onclick="test();"/> 

</head>

<body>

</body>

</html>

I've tried using jQuery, as well, but this still didn't help me:

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

Any suggestions?

jobrien9
  • 147
  • 2
  • 4
  • 10

5 Answers5

8

ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js") must run after you create the retrieveListItems function and before you call that retrieveListItems function.

jcp
  • 1,464
  • 1
  • 21
  • 36
YHTAN
  • 223
  • 2
  • 9
6

The correct syntax is to use the function name without parenthesis. The statement SP.SOD.executeFunc('sp.js', 'SP.ClientContext', retrieveListItems()); should be SP.SOD.executeFunc('sp.js', 'SP.ClientContext', retrieveListItems);. Similarly ExecuteOrDelayUntilScriptLoaded(retrieveListItems(), "sp.js") should be ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js")

Nadeem Yousuf-AIS
  • 18,707
  • 4
  • 28
  • 59
1

add these and your problem will be solved.

 <SharePoint:ScriptLink Name="sp.js" runat="server" LoadAfterUI="true" Localizable="false" />
 <SharePoint:ScriptLink Name="sp.runtime.js" runat="server" LoadAfterUI="true" Localizable="false" />
 <SharePoint:ScriptLink Name="sp.core.js" runat="server" LoadAfterUI="true" Localizable="false" />
Paul Strupeikis
  • 3,818
  • 4
  • 20
  • 33
Ons
  • 11
  • 1
0

Change the placeholder value for the url variable before you run the code. https://msdn.microsoft.com/en-us/library/office/jj245896.aspx

        var url = 'Your absolute URL';
        var clientContext;
        var website;

        // Make sure the SharePoint script file 'sp.js' is loaded before your
        // code runs.
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);

        // Create an instance of the client context for the specified URL.
        function sharePointReady() {
            clientContext = new SP.ClientContext(url);
            website = clientContext.get_web();

            clientContext.load(website);
            clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);
        }
        function onRequestSucceeded() {
            alert('URL of the website: ' + website.get_url());
        }
        function onRequestFailed(sender, args) {
            alert('Error: ' + args.get_message());
        }
-1

I have written a post related to this. Check out Using JavaScript or JQuery and JSOM in SharePoint

Prashanth
  • 49
  • 2
  • 1
    Please add some important part of your answer here because once the link broken it will be of no use to the community members. You can include some essential part here and give the reference as your link. – Aakash Maurya Jul 16 '16 at 03:39