Tuesday, 18 April 2017

Retrieve All the Files From Specific Folder Along with properties like ID, File Size,Editor etc

Below Code will fetch all the files under MyFolder in Documents library along with extended metadata like document id, modified by, document size and document url etc. ListItemAllFields will do the trick to get all the extended properties of the document.

var folderUrl  = "sites/sc1/Documents/MyFolder"
$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/GetFolderByServerRelativeUrl('" + folderUrl + "')/Files?$expand=ModifiedBy,ListItemAllFields",
    type: "Get",
    cache: false,
    async: true,
    crossDomain: true,
    beforeSend: function(xhr, settings) {
        // To do any operation before sending the request              
    },
    xhrFields: {
        withCredentials: true
    },
    dataType: "json",
    contentType: "application/json",

    success: function(data) {
        $.each(data.value, function(index, item) {
            var obj = {};
            obj["ID"] = item.ListItemAllFields.ID;
            obj["Name"] = itemN.ame;
            obj["LastUpdatedBy"] = item.ModifiedBy.Title;
            obj["LastUpdatedOn"] = item.TimeLastModified;
            var fileSize = item.Length / 1024;
            obj["Size"] = fileSize.toFixed() + "KB";
        });
    },
    error: function(err) {
       console.log("Error Occurred. : " + err);
     }
});

No comments:

Query List/Document Library in Specific Folder

To query SharePoint List or Document Library in specific Folder “ FolderServerRelativeUrl ” as part of the CAML Query Code Snippet ...