Search code examples
javascripthtmlsharepoint-2013sharepoint-rest-api

Load Content from SharePoint Library Folder by Created Date Order on Webpage using Javascript


I’m working with SharePoint and I’m able to load content from a library and display it on a webpage as a link, using REST API. I figured out a way to arrange the content in the order the documents are uploaded into the folder by the “Created Date”, displaying the last uploaded item on top. However, I feel there is a more efficient way of performing this action. The script I hacked together seem to work on occasion. Sometimes it will appear to be out of the sequence but when I refresh the browser it will return it in the items in the desire sequence. Below is the code I’m using to accomplish this task. If there is a better way of accomplishing this action will greatly be appreciated.

<ul id="column1">
    <h2 id="newsletter"><img style="width: 40px; position: relative; right: 5px; top: 7px;" src="../SiteAssets/SitePages/Test Page/icons/DCSA_Portal_Newsletter.svg" alt="logo">Newsletter<img id="arrow1" src="../SiteAssets/SitePages/Test Page/icons/arrow.png" alt="logo"></h2>
    <ol></ol>
</ul>

getFilesFromFolder("/sites/dcsa/ep/epMainFiles/News/Newsletter").done(function(data) {
    $.each(data.d.results, function(i, item) {

        var spDate = new Date(item.TimeCreated.split('T')[0]) //example: '2015-10-30T05:00:00Z'
        var newTillDate = new Date();
      spDate.setDate(spDate.getDate() + 5);

        if (spDate <= newTillDate) {

        $("#column1 ol").append('<li class="linkData newsletter" style="padding-left: 10px; padding-right: 10px;" data-date="' + item.TimeCreated.split('T')[0] + '"><a href="' + 'https://intelshare.intelink.gov' + item.ServerRelativeUrl + '" target="_blank">' + item.Name.replace(/\.[^/.]+$/, "") + " - " + '<span style="color: red;">' + item.TimeCreated.split('T')[0] + '</span>' + '</a></li>');

      } else {

         $("#column1 ol").append('<li class="linkData newsletter" style="padding-left: 10px; padding-right: 10px;" data-date="' + item.TimeCreated.split('T')[0] + '"><a href="' + 'https://intelshare.intelink.gov' + item.ServerRelativeUrl + '" target="_blank"><img class="newArrow" style="width: 60px; position: relative; right: 5px; top: 0px;"src="../SiteAssets/SitePages/Test Page/icons/arrow-with_new2.gif" alt="logo">' + item.Name.replace(/\.[^/.]+$/, "") + " - " + '<span style="color: red;">' + item.TimeCreated.split('T')[0] + '</span>' + '</a></li>');

     }
      col1_chgOrder();
    });
});

function col1_chgOrder() {
    var container = $("#column1 ol");
    var items = $("#column1 ol .linkData");

 items.each(function() {
     // Convert the string in 'data-event-date' attribute to a more
      // standardized date format
     var BCDate = $(this).attr("data-date");
     /*console.log(BCDate);
        var standardDate = BCDate[1]+" "+BCDate[0]+" "+BCDate[2];*/
     var standartDate = new Date(BCDate).getTime();
      $(this).attr("data-event-date", standartDate);
     //console.log(standartDate);
    });
 items.sort(function(a, b) {
        a = parseFloat($(a).attr("data-event-date"));
     b = parseFloat($(b).attr("data-event-date"));
        return a < b ? -1 : a > b ? 1 : 0;
    }).each(function() {
        container.prepend(this);
   });

}

function getFilesFromFolder(serverRelativeUrlToFolder) {
return $.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFolderByServerRelativeUrl('" + serverRelativeUrlToFolder + "')/files",
    method: "GET",
    async: false,
    headers: {
        "Accept": "application/json; odata=verbose"
    }
});
}

Solution

  • you could include orderby filter in your API endpoint url rather than doing sorting after response is received.

    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFolderByServerRelativeUrl('" + serverRelativeUrlToFolder + "')/files?&$orderby=Created desc
    

    i.e. you don't need to use col1_chgOrder() function as API will return files already sorted due to this parameter