Solved

SharePoint JSOM error

Posted on 2016-09-26
7
104 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
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
LVL 32

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 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
This article discusses how to create an extensible mechanism for linked drop downs.
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)

690 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