Introduction:
In this blog you will see how to get all the lists using KnockoutJS and CSOM in SharePoint 2013. Please refer my article to implement KnockoutJS in a SharePoint page.
Script:
  1. <script src="https://c986.sharepoint.com/SiteAssets/jquery-1.8.3.min.js"></script>
  2. <script src="https://c986.sharepoint.com/SiteAssets/knockout-3.1.0.js"></script>
  3. <script src="https://c986.sharepoint.com/SiteAssets/ko.sp-1.0.min.js"></script>
  4. <script>
  5. ExecuteOrDelayUntilScriptLoaded(MainFunction, "sp.js");
  6. var completeList = null;
  7. var listCollection=null;
  8. // Class used for saving the property values.
  9. function List(id, name) {
  10. var self = this;
  11. self.ID = id;
  12. self.Name = name;
  13. }
  14. // View Model - JavaScript that defines the data and behavior of your UI
  15. function ListViewModel() {
  16. var self = this;
  17. // observableArray equivalent of a regular array,
  18. self.Lists = ko.observableArray([]);
  19. self.AddLists = function (id,name) {
  20. self.Lists.push(new List(id,name));
  21. }
  22. }
  23. function MainFunction() {
  24. completeList = new ListViewModel();
  25. // Retrieve the SharePoint Site Content Types
  26. retrieveLists();
  27. // Activates knockout.js
  28. ko.applyBindings(completeList);
  29. }
  30. function retrieveLists() {
  31. var clientContext = new SP.ClientContext.get_current();
  32. var web = clientContext.get_web();
  33. this.listCollection = web.get_lists();
  34. clientContext.load(listCollection);
  35. clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
  36. }
  37. function onQuerySucceeded(sender, args) {
  38. var listsEnumerator = listCollection.getEnumerator();
  39. while (listsEnumerator.moveNext()) {
  40. var list = listsEnumerator.get_current();
  41. completeList.AddLists(list.get_id(), list.get_title());
  42. }
  43. }
  44. function onQueryFailed(sender, args) {
  45. alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
  46. }
  47. </script>
  48. <!-- view - HTML markup that defines the appearance of your UI -->
  49. <div id="divList">
  50. <h2>
  51. SharePoint Lists</h2>
  52. <br />
  53. <table id="tblList" border="1">
  54. <thead>
  55. <tr>
  56. <th align="center">
  57. ID
  58. </th>
  59. <th align="center">
  60. Name
  61. </th>
  62. </tr>
  63. </thead>
  64. <!-- Iterating through every list item using foreach of KO -->
  65. <tbody data-bind="foreach: Lists">
  66. <tr>
  67. <td data-bind="text: ID">
  68. </td>
  69. <td data-bind="text: Name">
  70. </td>
  71. </tr>
  72. </tbody>
  73. </table>
  74. </div>
Output:
Summary:
Thus in this blog you saw how to get all the lists using KnockoutJS and CSOM in SharePoint 2013.

Comments

Join the conversation! Your thoughts help the community grow.