In this article I would like to share the code to get the list items from the app host web site collection in a SharePoint Hosted app using JavaScript.

Use the following code to get the items:

  1. var context = SP.ClientContext.get_current();
  2. var user = context.get_web().get_currentUser();
  3. var hostWebUrl;
  4. var appWebUrl;
  5. var listName = "NewsList";
  6. var itemCollection;
  7. var lists;
  8. var ctx;
  9. var appCtxSite;
  10. var listEnumerator;
  11. var currentlist;
  12. var list;
  13. var currentListItems;
  14. var itemTitle;
  15. var web;
  16. var siteUrl;
  17. // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
  18. $(document).ready(function ()
  19. {
  20. hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
  21. appWebUrl = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));
  22. getListItemFromHostWeb();
  23. });
  24. //This function is used to get the hostweb url
  25. function manageQueryStringParameter(paramToRetrieve)
  26. {
  27. var params =
  28. document.URL.split("?")[1].split("&");
  29. var strParams = "";
  30.   for (var i = 0; i < params.length; i = i + 1)
  31.    {
  32. var singleParam = params[i].split("=");
  33. if (singleParam[0] == paramToRetrieve)
  34.     {
  35. return singleParam[1];
  36. }
  37. }
  38. }
  39. //get the list data from host web
  40. function getListItemFromHostWeb() {
  41. debugger;
  42. ctx = new SP.ClientContext(appWebUrl);
  43. appCtxSite = new SP.AppContextSite(ctx, hostWebUrl);
  44. web = appCtxSite.get_web();
  45. lists = web.get_lists();
  46. ctx.load(lists);
  47. ctx.executeQueryAsync(IsListExist, OnGetListItemFailure);
  48. }
  49. //check whether list is exist or not
  50. function IsListExist() {
  51. debugger;
  52. //alert('check whether list is exist or not');
  53. var isListAvail = false;
  54. listEnumerator = lists.getEnumerator();
  55. while (listEnumerator.moveNext()) {
  56. currentlist = listEnumerator.get_current();
  57. if (currentlist.get_title() == listName) {
  58. isListAvail = true;
  59. //Read list items from the lists
  60. getListItemCollection();
  61. }
  62. }
  63. if (!isListAvail) {
  64. alert('News List not available in this site');
  65. }
  66. }
  67. //getItemsCollection from sp host web list
  68. function getListItemCollection() {
  69. debugger;
  70. list = lists.getByTitle(listName);
  71. var camlQuery = new SP.CamlQuery();
  72. camlQuery.set_viewXml("<View>" + "<RowLimit>2</RowLimit>" + "</View>");
  73. itemCollection = list.getItems(camlQuery);
  74. ctx.load(itemCollection);
  75. ctx.executeQueryAsync(OnGetListItemSuccess, OnGetListItemFailure);
  76. }
  77. // Get Items from List Item collection
  78. function OnGetListItemSuccess() {
  79. debugger;
  80. var imageStr = "";
  81. if (itemCollection.get_count() > 0) {
  82. var enumerator = itemCollection.getEnumerator();
  83. while (enumerator.moveNext()) {
  84. currentListItems = enumerator.get_current();
  85. itemTitle = currentListItems.get_item("Title");
  86. var itemImageURL = currentListItems.get_item("ImageUrl");
  87. document.getElementById('lblTitle').innerHTML += itemTitle + ",";
  88. if (itemImageURL != "" && itemImageURL!=null) {
  89. imageStr += "<img src='" + itemImageURL.get_url() + "' alt='No Image' />";
  90. }
  91. }
  92. document.getElementById('image').innerHTML = imageStr;
  93. }
  94. }
  95. function OnGetListItemFailure(sender, args) {
  96. alert('Failed to get user name. Error:' + args.get_message());
  97. }
note

In the AppManifest.xml file give Read permission to the SiteCollection.

Summary

In this article we explored how to retrieve list items in a SharePoint Hosted app from an app host web list.