1

I tried the below code from MSDN

function retrieveListItems() {

    var clientContext = new SP.ClientContext('http://www.vignesh.cloudappsportal.com');
    var oList = clientContext.get_web().get_lists().getByTitle('EmailId');
    clientContext.load(oList);

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    function onQuerySucceeded(sender, args) {
        SPListItems.push(sender); // pushing values into a global array to check in console
        SPListItems.push(args);
    }

    function onQueryFailed(sender, args) {
        alert('fail')
    }
}

oList is getting some values, but i get error in this.onQuerySucceeded

this is referring to window and so there is no function like this.onQuerySucceeded

6
  • Are you running the JavaScript within SharePoint environment? Commented Apr 21, 2015 at 5:19
  • And are there a list with the title EmailId? Commented Apr 21, 2015 at 5:21
  • Are you using plane .aspx page (i.e. page without sharepoint master page) ? Commented Apr 21, 2015 at 5:23
  • @ArsalanAdamKhatri yeah, the script is inside sharepoint app Commented Apr 21, 2015 at 5:24
  • @RohitWaghela its a sharepoint app Commented Apr 21, 2015 at 5:24

2 Answers 2

5

You are missing a couple of rows in a working script. First, after setting the oList variable you need to load it:

clientContext.load(oList);

Then you need to fetch the value by calling:

clientContext.executeQueryAsync(
    Function.createDelegate(this, onQuerySucceeded), 
    Function.createDelegate(this, onQueryFailed)
);

Now, you need to create the following two methods (for success and failure of the execution):

 function onQuerySucceeded(sender, args){
     // here you can work with oList
 }

 function onQueryFailed(sender, args) {
    alert('fail')
 }
4
  • Thanks @robert will the args in onQuerySucceeded have the list of items under the column? Commented Apr 21, 2015 at 5:36
  • Please check my updated question Commented Apr 21, 2015 at 5:41
  • this is referring to window when i checked in console Commented Apr 21, 2015 at 5:52
  • 1
    Remove the thisfrom the create delegate, as I have done in my update answer Commented Apr 21, 2015 at 7:16
1

If your ultimate goal is to get SharePoint list items using JavaScript, then you can refer below set of functions.

Also you can refer this useful msdn blog.

        function getListItems () {
            try {
                var clientContext = new SP.ClientContext('http://www.vignesh.cloudappsportal.com');
                var oList = clientContext.get_web().get_lists().getByTitle('EmailId');
                var camlQuery = new SP.CamlQuery();
                this.collListItems = oList.getItems(camlQuery);
                clientContext.load(collListItems); 
                clientContext.executeQueryAsync(onSuccess, onFail);
            }
            catch (err) {
                alert('Error while getting list items');
            }
        }

        function onSuccess () {
            var listItemEnumerator = collListItems.getEnumerator();
            while (listItemEnumerator.moveNext()) {
                oListItem = listItemEnumerator.get_current();
                alert(oListItem.get_item('Title'));
            }
        }

        function onFail (sender, args) {
            alert(args.get_message());
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.