In this article I would like to share the code to get the Image from an APP host web picture library in a SharePoint hosted APP.

Use the following code to get an image from a library using the JavaScript object model:

Variables declaration:
  1. var libraryName = "NewsPictureLibrary";
  2. var hostWebUrl;
  3. var appWebUrl;
  4. var ctx;
  5. var appCtxSite;
  6. var web;
  7. var libraries;
  8. var listEnumerator;
  9. var currentlibrary;
  10. var library;
  11. //This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
  12. $(document).ready(function () {
  13. // Get the URI decoded URLs.
  14. hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
  15. appWebUrl = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));
  16. //calling the get image library method in ready function to get image on when page is ready
  17. getLibraryfromHostweb();
  18. });
  19. //This function is used to get the host web url
  20. function manageQueryStringParameter(paramToRetrieve) {
  21. var params =
  22. document.URL.split("?")[1].split("&");
  23. var strParams = "";
  24. for (var i = 0; i < params.length; i = i + 1) {
  25. var singleParam = params[i].split("=");
  26. if (singleParam[0] == paramToRetrieve) {
  27. return singleParam[1];
  28. }
  29. }
  30. }
  31. //Get Image from Picture Library
  32. function getLibraryfromHostweb ()
  33. {
  34. //Get the app web context
  35. ctx = new SP.ClientContext(appWebUrl);
  36. //Get the app web context
  37. appCtxSite = new SP.AppContextSite(ctx, hostWebUrl);
  38. //get current web from host web
  39. web = appCtxSite.get_web();
  40. //get all libraries from web
  41. libraries = web.get_lists();
  42. // .load() tells CSOM to load the properties of this object
  43. ctx.load(libraries);
  44. // now start the asynchronous call and perform all commands
  45. ctx.executeQueryAsync(IsListExist, OnGetListItemFailure);
  46. // method will exit here and IsListExist or OnGetListItemFailure will be called asynchronously
  47. }
  48. //check whether list is exist or not
  49. function IsListExist() {
  50. debugger;
  51. //alert('check whether list is exist or not');
  52. var isListAvail = false;
  53. listEnumerator = libraries.getEnumerator();
  54. while (listEnumerator.moveNext()) {
  55. currentlibrary = listEnumerator.get_current();
  56. //check whether the library name is equal to current library
  57. if (currentlibrary.get_title() == libraryName) {
  58. isListAvail = true;
  59. //Retrieve list items from the lists
  60. getListItemCollection();
  61. }
  62. }
  63. if (!isListAvail) {
  64. alert('News Library is not available in this site');
  65. }
  66. }
  67. //Get image collection from Library
  68. function getListItemCollection() {
  69. debugger;
  70. //Get the library by Title
  71. library = libraries.getByTitle(libraryName);
  72. //get two items on library using caml Query
  73. var camlQuery = new SP.CamlQuery();
  74. camlQuery.set_viewXml("<View>" + "<RowLimit>2</RowLimit>" + "</View>");
  75. //returns the item collectionbased on the query.
  76. // “getItems” is one of the method that is used to retrieve the items from the list using the listitem object. This method specifies which items to return.
  77. itemCollection = library.getItems(camlQuery);
  78. ctx.load(itemCollection);
  79. ctx.executeQueryAsync(OnGetListItemSuccess, OnGetListItemFailure);
  80. }
  81. //Get Image name from Library
  82. function OnGetListItemSuccess() {
  83. debugger;
  84. var imageStr = "";
  85. var ImageName;
  86. //get_count() is used to get the items from current library
  87. if (itemCollection.get_count() > 0) {
  88. var enumerator = itemCollection.getEnumerator();
  89. while (enumerator.moveNext()) {
  90. var currentListItems = enumerator.get_current();
  91. //get Image Name from current Item
  92. ImageName = currentListItems.get_item("FileLeafRef");
  93. }
  94. }
  95. }
  96. //Failure method
  97. function OnGetListItemFailure(sender, args) {
  98. alert('Failed to get user name. Error:' + args.get_message());
  99. }
note

In the AppManifest.xml file give read permission to the Site Collection.

Summary

In this article we explored how to get an image from a SharePoint app host web picture library using the JavaScript object model in a SharePoint Hosted App.