Create Dynamic Photos Page Using Rest API And SharePoint

Hi All,

In this blog, we will see how to create a dynamic page which includes all photos of the particular event.

Some days before we celebrated Indian Independence day in our office. I want to post all the photos on one web part page.

Initially, I thought to upload it in content editor web part one by one using Image properties. But Later I Thought what will happen if I receive more images. I need to edit the web part as soon as I receiver more images. After that, I decided to create one library to upload the photos and using REST API I will show the Images in the section.

Below are the steps,
  1. Create document library with the name "IndepedanceDayPics". In that create a column with the name "Seq" and having type number to show the photos sequentially.

  2. Upload all the photos in that document library and using quick edit functionality add Sequence numbers also.

  3. Create one web part page having the name "Celebration".

  4. Create one txt file and add below code in that file,

    add Refernce of jQuery library,
    1. <script src="/SiteAssets/JS/jquery.min.js">  
    2. </script> add style for the table and other contents  
    3. <style>  
    4.     div#contentBox {  
    5.         margin-left: 50px !important;  
    6.     }  
    7.   
    8.     div#headerdiv {  
    9.         text-align: center;  
    10.         font-size: 30px;  
    11.         font-weight: 700;  
    12.         color: #D75517;  
    13.     }  
    14.  
    15.     #pics td {  
    16.         width: 30%;  
    17.     }  
    18.   
    19.     table#pics {  
    20.         width: 100%;  
    21.         margin-top: 30px;  
    22.     }  
    23.  
    24.     #picID {  
    25.         width: 400px;  
    26.         height: 350px;  
    27.     }  
    28. </style>  
    29. <div id="headerdiv"> Independence Day Celebration </div>  
    30. <table id="pics">  
    31. </table>  
    32. <script>  
    33.     $(document).ready(function() {  
    34.         $.ajax({  
    35.             url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('IndepedanceDayPics')/items?$select=FileRef/FileRef&$orderby=Seq asc",  
    36.             type: "GET",  
    37.             headers: {  
    38.                 "accept""application/json;odata=verbose"  
    39.             },  
    40.             success: function(data) {  
    41.                 var inputElement = "";  
    42.                 if (data.d.results.length > 0) {  
    43.                     var i = 0;  
    44.                     while (i < data.d.results.length) {  
    45.                         var j = 0;  
    46.                         inputElement += "<tr>";  
    47.                         while (j < 3 && i < data.d.results.length) {  
    48.                             inputElement += "<td><img id='picID' src='" + data.d.results[i].FileRef + "'></td>";  
    49.                             j++;  
    50.                             i++;  
    51.                         }  
    52.                         inputElement += "</tr>";  
    53.                     }  
    54.                 }  
    55.                 $("#pics").append(inputElement);  
    56.             },  
    57.             error: function(xhr) {  
    58.                 alert(xhr.status + ': ' + xhr.statusText);  
    59.             }  
    60.         });  
    61.     });  
    62. </script>  
  5. Now edit "Celebration.aspx" page and add content editor web part. After adding content editor web part edit the web part and give path of that txt file. Click on Apply and OK button.

  6. Now, save the page and Publish it.

  7. After publishing the page it will show all the images in the tabular format. It will create One table and will show 3 images in a row and so on.
Thanks