• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 134
  • Last Modified:

SharePoint JSOM error

I'm not sure why I get the following error: ""TypeError: Object doesn't support this action\n   at getListData (http://../demos/JSOM/Webparts/JSOM/JSOMGetListData.js:10:5)\n   at Anonymous function (http://../demos/JSOM/Webparts/JSOM/JSOMGetListData.js:3:5)\n   at j (https://code.jquery.com/jquery-3.1.1.min.js:2:29941)\n   at Anonymous function (https://code.jquery.com/jquery-3.1.1.min.js:2:30262)""

I'm just trying to get data from a SharePoint 2013 list
Here's my code

JSOMGetListData.html:
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="http://../demos/JSOM/Webparts/JSOM/JSOMGetListData.js"></script>

Open in new window


JSOMGetListData.js:

// JavaScript source code
$(function() {
    getListData();
});

function getListData() {
    //Entry point for all JSOM calls.
    //It holds a lot of information about the context of the current site, user, web, and so forth.
    //Will return the context of the current site.
    var clientContext = new SP.clientContext();

    /*Gives us the current web or site in the site collection the user is executing the code from.*/
    var oWebsite = clientContext.get_web();
                   
    /*Grabs all the lists from oWebsite variable. 'this' is used instead of 'var' so that collList can be used globally*/
    this.collList = oWebsite.get_lists();

    /*the query is getting loaded into the client context to send to SharePoint.*/
    clientContext.load(collList);

    /*executeQueryAsync actually sends all the requests to SharePoint for processing*/
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded),
        Function.createDelegate(this, this.onQueryFailed)
        );
}

function onQerySucceeded() {
    var listInfo = '';
    var listEnumerator = collList.getEnumerator();

    while (listEnumerator.moveNext()) {
        var oList = listEnumerator.get_current();
        listInfo += 'Title: ' + oList.get_title() + '- Created: ' + oList.get_created().toString() + '<br />';
    }
    $("#divGetListData").html(listInfo);
}

function onQueryFailed(sender, args) {
    alert('Requet failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

Open in new window

0
Isaac
Asked:
Isaac
  • 4
  • 2
1 Solution
 
Jamie McAllister MVPArchitectCommented:
You should be using get_current on the client context or providing a site URL. It has nothing to work with as it's instantiated right now.
0
 
Bhupendra RayateCommented:
You can call the function when sp.js loaded using executes or delay until script loaded and add site url in client context.
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Tried it but not working.  I also tried adding the site url but no luck.

// JavaScript source code
$(function() {
    getListData();
});
alert("before function");
function getListData() {
    //Entry point for all JSOM calls.
    //It holds a lot of information about the context of the current site, user, web, and so forth.
    //Will return the context of the current site.
    alert("in function");
    var clientContext = new SP.ClientContext.get_current();
    alert("clienContext: "+clientContext);
    
    /*Gives us the current web or site in the site collection the user is executing the code from.*/
    var oWebsite = clientContext.get_web();
    alert("oWebsite: "+ oWebsite);
    /*Grabs all the lists from oWebsite variable. 'this' is used instead of 'var' so that collList can be used globally*/
    this.collList = oWebsite.get_lists();
alert("this.collList: "+this.collList);
    /*the query is getting loaded into the client context to send to SharePoint.*/
    clientContext.load(collList);
alert("collList: "+collList);
    /*executeQueryAsync actually sends all the requests to SharePoint for processing*/
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded),
        Function.createDelegate(this, this.onQueryFailed)
        );
}

function onQerySucceeded() {
alert("querySucceed");
    var listInfo = '';
    var listEnumerator = collList.getEnumerator();

    while (listEnumerator.moveNext()) {
        var oList = listEnumerator.get_current();
        listInfo += 'Title: ' + oList.get_title() + '- Created: ' + oList.get_created().toString() + '<br />';
    }
    alert(listInfo);
    $("#divGetListData").html(listInfo);
}

function onQueryFailed(sender, args) {
    alert('Requet failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Jamie McAllister MVPArchitectCommented:
I'm looking at this more, but one thing I noticed first. There's a typo in the Query Suceeded delegate function name.

"onQerySucceeded"
0
 
Jamie McAllister MVPArchitectCommented:
I only have a 2010 sandbox to hand. Pasted the below code into a CEWP and it works fine.

So the current code is good. It may be a timing issue or problem with the references to the JS libraries.

<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">


// JavaScript source code

alert("before function");
function getListData() {
    //Entry point for all JSOM calls.
    //It holds a lot of information about the context of the current site, user, web, and so forth.
    //Will return the context of the current site.
    alert("in function");
    var clientContext = new SP.ClientContext.get_current();
    alert("clienContext: "+clientContext);
    
    /*Gives us the current web or site in the site collection the user is executing the code from.*/
    var oWebsite = clientContext.get_web();
    alert("oWebsite: "+ oWebsite);
    /*Grabs all the lists from oWebsite variable. 'this' is used instead of 'var' so that collList can be used globally*/
    this.collList = oWebsite.get_lists();
alert("this.collList: "+this.collList);
    /*the query is getting loaded into the client context to send to SharePoint.*/
    clientContext.load(collList);
alert("collList: "+collList);
    /*executeQueryAsync actually sends all the requests to SharePoint for processing*/
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded),
        Function.createDelegate(this, this.onQueryFailed)
        );
}

function onQuerySucceeded() {
alert("querySucceed");
    var listInfo = '';
    var listEnumerator = collList.getEnumerator();

    while (listEnumerator.moveNext()) {
        var oList = listEnumerator.get_current();
        listInfo += 'Title: ' + oList.get_title() + '- Created: ' + oList.get_created().toString() + '<br />';
    }
    alert(listInfo);
    $("#divGetListData").html(listInfo);
}

function onQueryFailed(sender, args) {
    alert('Requet failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

$(function() {
    getListData();
});

</script>

Open in new window

0
 
Jamie McAllister MVPArchitectCommented:
OK tell a lie, I managed to get a timing error outside of edit view. Inserted a ExecuteOrDelayUntilScriptLoaded bit and this now works for me...

<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
// JavaScript source code

alert("before function");
function getListData() {
    //Entry point for all JSOM calls.
    //It holds a lot of information about the context of the current site, user, web, and so forth.
    //Will return the context of the current site.
    alert("in function");
    var clientContext = new SP.ClientContext.get_current();
    alert("clienContext: "+clientContext);
    
    /*Gives us the current web or site in the site collection the user is executing the code from.*/
    var oWebsite = clientContext.get_web();
    alert("oWebsite: "+ oWebsite);
    /*Grabs all the lists from oWebsite variable. 'this' is used instead of 'var' so that collList can be used globally*/
    this.collList = oWebsite.get_lists();
alert("this.collList: "+this.collList);
    /*the query is getting loaded into the client context to send to SharePoint.*/
    clientContext.load(collList);
alert("collList: "+collList);
    /*executeQueryAsync actually sends all the requests to SharePoint for processing*/
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded),
        Function.createDelegate(this, this.onQueryFailed)
        );
}

function onQuerySucceeded() {
alert("querySucceed");
    var listInfo = '';
    var listEnumerator = collList.getEnumerator();

    while (listEnumerator.moveNext()) {
        var oList = listEnumerator.get_current();
        listInfo += 'Title: ' + oList.get_title() + '- Created: ' + oList.get_created().toString() + '<br />';
    }
    alert(listInfo);
    $("#divGetListData").html(listInfo);
}

function onQueryFailed(sender, args) {
    alert('Requet failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

jQuery(document).ready(function() {
 ExecuteOrDelayUntilScriptLoaded(getListData, "sp.js");
});

</script>

Open in new window

0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Thanks for catching that.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now