4

I'm trying to load in documents from the Documents library in SharePoint and I want to be able to filter them by file type (Word Doc, Excel Sheet, etc.) from my application. I've figured out how to get the icon for the file type using mapToIcon, but I can't seem to find anywhere how to get the file type.

Is there any function or API call to get the file type of a file? I would prefer to not have to hard code each type based on file extension but I may need to do that.

Here is a bit of my code for reference

// data returned from an ajax call to get all documents
var results = data.d.results;

for(var i = 0; i < results.length; i++){
    // get the name of the document
    var name = results[i].Name;

    // retrieve the icon for the file type through an ajax call
    var icon;
    $.ajax({
            url: siteUrl + "/_api/web/maptoicon(filename='" + name + "',progid='',size=0)"
            /*headers*/,
            success: function(data){
                // get the icon
                icon = data.d.MapToIcon;
            },  
        });
    }
7

You have to add one more attribute [File_x0020_Type] to your REST query select parameter :

_spPageContextInfo.siteAbsoluteUrl + /_api/web/lists/getbytitle('<list title>')/items?$select=File_x0020_Type"

Result:

enter image description here

1
  • Thanks for the help. Unfortunately, it didn't work for me. See my answer to see what did work
    – Matt L.
    Jul 18 '18 at 14:19
3

Since the files I was retrieving were not part of a list, and rather a document library, the following URL is what worked for me:

siteUrl + "/_api/web/GetFolderByServerRelativeUrl('Shared%20Documents')/Files?$select=ListItemAllFields/File_x0020_Type&$expand=ListItemAllFields

Result: enter image description here

Reference Post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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