[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SharePoint JSOM error

Posted on 2016-09-26
7
Medium Priority
?
126 Views
Last Modified: 2016-09-29
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
Comment
Question by:Isaac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 32

Expert Comment

by:Jamie McAllister MVP
ID: 41817556
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
 

Expert Comment

by:Bhupendra Rayate
ID: 41819336
You can call the function when sp.js loaded using executes or delay until script loaded and add site url in client context.
0
 
LVL 5

Author Comment

by:Isaac
ID: 41821039
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 32

Accepted Solution

by:
Jamie McAllister MVP earned 2000 total points
ID: 41821353
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
 
LVL 32

Expert Comment

by:Jamie McAllister MVP
ID: 41821374
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
 
LVL 32

Expert Comment

by:Jamie McAllister MVP
ID: 41821392
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
 
LVL 5

Author Closing Comment

by:Isaac
ID: 41821801
Thanks for catching that.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question