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?

2
  • It doesn't look like you're loading all the assets you need -- which version of SharePoint? – John-M Dec 15 '14 at 14:56
  • SharePoint 2010 – jobrien9 Dec 15 '14 at 22:40
8

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

0
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")

1
  • Thanks, Nadeem. I tried both and neither worked. Would you happen to have any other ideas? – jobrien9 Dec 16 '14 at 3:31
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" />
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

1
  • 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 3:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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