0

I have used below code for getting list items from SharePoint list. Output is coming in alerts but it is not displayed in HTML controls.

<html lang="en-US">
<head>
    <meta charset="UTF-8">
</head>
<script src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-2.2.1.js" type="text/javascript"></script>
<script src="http://code.jquery.com/jquery-1.7.1.js"></script>
<script>

    function insightViewModel() {
        var self = this;

        self.insights = ko.observableArray();
            $.ajax({
                url: "http://demo/Dev/_api/web/lists/getbytitle('CountryNames')/items",
                type: "GET",
                headers: {
                    "accept": "application/json;odata=verbose",
                },
                success:suc,
                error: function (error) {
                    alert(JSON.stringify(error));
                    }

                });

            function suc(data)
                {
                alert(data.d.results);
                r=data.d.results;
                alert(r[1].CountryName);

                                for(var i=0;i<5;i++)
                                    {
                                                          self.insights.push({
                                                           Title:ko.observable(r[i].Title), 
                                                           CountryName:ko.observable(r[i].CountryName)
                                                           });
                                 alert(r[i].CountryName);
                                     }
                }

                            }           

         ko.applyBindings(new insightViewModel());

</script>
<body>
    <table>
    <thead>
        <tr><th>Title</th><th>Name</th></tr>
    </thead>
    <tbody data-bind="foreach:insights">
        <tr>

            <td data-bind="text:CountryName"></td>
        </tr>
    </tbody>
</table>
</body>
</html>

This is the output of data.d.results from sharepoint site. I have used this code in script editor.

enter image description here

1 Answer 1

1

Your code works fine, check this fiddle: http://jsfiddle.net/n869w8yh/

I replaced your request to SharePoint with a example array.

Just make sure you type the names of the fields correctly, don't forget case sensitivity. The for-loop is hard coded to 5 items in your code, do you have 5 items in that list?

EDIT:

Finally got some time to test this in a script editor. Try this in your code:

$(document).ready(function() {
  ko.applyBindings(new insightViewModel());
});
5
  • yes I have 5 items
    – KumarV
    Nov 4, 2015 at 10:04
  • 1
    Could you update the question with the result you're getting back from SharePoint? I would like to see what's in data.d.result. Nov 4, 2015 at 10:14
  • I see that you have updated the question with the result of data.d.results. I should have been more precise, how does one of those object look? Do they contain both the attributes CountryName AND Title? You can use JSON.stringify(data.d.results[0]) or alert(data.d.results[0].CountryName + " - " + data.d.results[0].Title); Nov 4, 2015 at 10:39
  • I have used this, " alert(data.d.results[0].CountryName + " - " + data.d.results[0].Title); " and it is giving me the results like " india - hello" from sharepoint list
    – KumarV
    Nov 4, 2015 at 10:48
  • @vikashkumar please see my updated answer. Nov 5, 2015 at 18:32

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.