0

I am using REST to obtain a list of files in a specific folder within a document library. I am using the GetFolderByServerRelativeUrl endpoint and the call is returning the correct number of documents - 6 for my test folder. I can access the Name parameter (and UI Version which I don't even know what that is but it is constant for all files) for the returned files but nothing else and I don't know why. You can see some of the fields I have tried to log to the console before I get on to the proper coding. Any guidance would be appreciated.

Code is as follows:

var APIUrl = _spPageContextInfo.siteAbsoluteUrl + "/_api/web/GetFolderByServerRelativeUrl('Document Library/Folder')/Files"

console.log(APIUrl);

var call = $.ajax({
    url: APIUrl,
    type: "GET",
    dataType: "JSON",
    headers: 
        {
            Accept:"application/json; odata=verbose"
        },
    success: function(data) {  
        $.each(data.d.results, function(index, item){ 
            console.log(index + ':' + item.UIVersion);
            console.log(index + ':' + item.ContentTypeID);
            console.log(index + ':' + item.DocumentIDValue);
            console.log(index + ':' + item.ID);
            console.log(index + ':' + item.GUID);
            console.log(index + ':' + item.Name);
        });
    },  
    error: function(data) {  
        console.log("An error occurred. Please try again.");  
    }  
});
1
  • Console.log(item) -> this will print an object which gives the result of whatever you get for a specific file. Check if req properties are there. Commented Oct 31, 2019 at 12:04

2 Answers 2

0

SharePoint has a "split personality" when it comes to working with libraries/files. There is some information available about the files at the endpoint you're using, but the rest of the information is stored in a hidden list.

I recommend using a tool like Postman or Talend API tester to help develop your statements, it allows a lot more freedom to change things and makes for easier inspection of the returned results. Or at least learning how to use the debug tools in a browser like Chrome.

Background

Using those tools I'm looking at the information that comes back from that endpoint. I get the following "simple" (by that I mean string or number) properties: CheckInComment, ChecOutType, ContentTag, CustomizedPageStatus, Etag, Exists, Length, Level, MajorVersion, MinorVersion, Name, ServerRelativeURL, TimeCreated, TimeLastModified, Title, UIVersion, and UIVersionLabel.

There are also some complex ones that are an object and contain useful information, for example Author.__deferred.uri which looks like this (/_api/Web/GetFileByServerRelativeUrl('/path/toFile')/Author) and fetches information about the author of the document.

Okay so here is your Answer

There is also another __deferred.uri called ListItemAllFields and structured like this /_api/Web/GetFileByServerRelativeUrl('/path/to/document')/ListItemAllFields that will get all the associated metadata -- built in stuff like the ID, GUID, or ContentyType. Also, because 'SharePoint,' the document ID value is called OData__dlc_DocId.

So basically you probably need to make one call to get the information about the folder contents and then an additional call for each result to get the associated metadata. There might also be some different ways to approach it depending upon what you're trying to do.

0

ID, GUID, ContentTypeID and DocumentID is file metadata and it's belong to the item endpoint, if you want to get these value for the files in a specific sub folder, you could filter with FileDirRef which stores the file location folder:

<script type="text/javascript">
var APIUrl = _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbyTitle('doc2')/items?$filter=FileDirRef eq '/sites/dev/doc2/folder1'";
console.log(APIUrl);

var call = $.ajax({
    url: APIUrl,
    type: "GET",
    dataType: "JSON",
    headers: 
        {
            Accept:"application/json; odata=verbose"
        },
    success: function(data) {  
    console.log(data.d.results);   
     $.each(data.d.results, function(index, item){ 

            console.log(index + ':' + item.OData__UIVersionString);
            console.log(index + ':' + item.ContentTypeID);
            console.log(index + ':' + item.OData__dlc_DocId);
            console.log(index + ':' + item.ID);
            console.log(index + ':' + item.GUID);
         });
    },  
    error: function(data) {  
        console.log("An error occurred. Please try again.");  
    }  
});

</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.