Solved

SharePoint JSOM error

Posted on 2016-09-26
7
41 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
  • 4
  • 2
7 Comments
 
LVL 31

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 31

Accepted Solution

by:
Jamie McAllister MVP earned 500 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 31

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 31

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C3-->D3 Line Chart 4 19
Javascript: Find and Enable Submit Button in Table 3 28
JavaScript error 1 34
onOpen 14 43
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now