Please follow the below steps:

  1. Create a list in the main site (host web) as below:

    Create a list

  2. Open Visual Studio -> Create a new SharePoint App Project.

    SharePoint-Hosted

  3. Select the host for the app as “SharePoint-Hosted”.

  4. Once the project is created, you will find the below structure.

    testApp

  5. In the default.aspx add reference to knockout.js file [the file has been downloaded and added to the project under Scripts folder].
    1. <scripttype="text/javascript"src="../Scripts/knockout-2.2.1.js"></script>
    Add the below code as well in the default.aspx,
    1. <tableborder="1" cellspacing="0" cellpadding="10">
    2. <theadstyle='background-color:gray;color:white'>
    3. <tr>
    4. <td>Name</td>
    5. <td>Roll No</td>
    6. </tr>
    7. </thead>
    8. <tbodydata-bind="foreach: assignments">
    9. <tr>
    10. <tddata-bind="text: personName">
    11. </td>
    12. <tddata-bind="text: rollNo">
    13. </td>
    14. </tr>
    15. </tbody>
    16. </table>
  6. Add the below code to the App.js file.
    1. 'use strict';
    2. var appWebContext;
    3. var hostweburl;
    4. $(document).ready(function ()
    5. {
    6. ViewGrid();
    7. });
    8. function ViewGrid()
    9. {
    10. ko.applyBindings(new TestListViewModel());
    11. }
    12. function TestListViewModel()
    13. {
    14. var self = this;
    15. appWebContext = new SP.ClientContext.get_current();
    16. hostweburl = decodeURIComponent($.getUrlVar("SPHostUrl"));
    17. var hostwebContext = new SP.AppContextSite(appWebContext, hostweburl);
    18. self.assignments = ko.observableArray();
    19. self._loadList = function ()
    20. {
    21. var clientContext = SP.ClientContext.get_current();
    22. var list = hostwebContext.get_web().get_lists().getByTitle('TestList');
    23. var query = new SP.CamlQuery();
    24. query.set_viewXml("<View>" + " <Query>" + " <OrderBy>" + " <FieldRef Name='Modified' Ascending='True' />" + " </OrderBy>" + " </Query>" + " <ViewFields>" + " <FieldRef Name='Modified' />" + " <FieldRef Name='Name' />" + " <FieldRef Name='RollNo' />" + " </ViewFields>" + "</View>");
    25. self._pendingItems = list.getItems(query);
    26. clientContext.load(self._pendingItems);
    27. clientContext.executeQueryAsync(Function.createDelegate(self, self._onLoadListSucceeded), Function.createDelegate(self, self._onLoadListFailed));
    28. }
    29. self._onLoadListSucceeded = function (sender, args)
    30. {
    31. var listEnumerator = self._pendingItems.getEnumerator();
    32. while (listEnumerator.moveNext())
    33. {
    34. var item = listEnumerator.get_current().get_fieldValues();
    35. self.assignments.push(
    36. {
    37. rollNo: item.RollNo.toString(),
    38. personName: item.Name.toString(),
    39. });
    40. }
    41. }
    42. self._onLoadListFailed = function (sender, args)
    43. {
    44. alert('Unable to load file list: ' + args.get_message() + '\n' + args.get_stackTrace());
    45. }
    46. self._loadList();
    47. }
    48. // jQuery plugin for fetching querystring parameters
    49. jQuery.extend(
    50. {
    51. getUrlVars: function ()
    52. {
    53. var vars = [],
    54. hash;
    55. var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    56. for (var i = 0; i < hashes.length; i++)
    57. {
    58. hash = hashes[i].split('=');
    59. vars.push(hash[0]);
    60. vars[hash[0]] = hash[1];
    61. }
    62. return vars;
    63. },
    64. getUrlVar: function (name)
    65. {
    66. return jQuery.getUrlVars()[name];
    67. }
    68. });
  7. Go to AppManifest.xml -> Permissions tab. We need to give permission to the app to access the host web. Select “SiteCollection”-> Permission Level ”Manage” (I have given manage, since I am planning to edit the data as well, which I will do late.) Read permission can also be given.

  8. Deploy the solution, trust the app.

  9. The Host-web list data will show up as below.

    Run