如何在 Sharepoint 中获取文件的文件类型

How to get the File Type of a File in Sharepoint

我正在尝试从 SharePoint 的文档库中加载文档,我希望能够按文件类型(Word Doc、Excel Sheet 等)从我的应用程序。我已经想出如何使用 mapToIcon 获取文件类型的图标,但我似乎无法在任何地方找到如何获取文件类型。

是否有任何函数或API调用来获取文件的文件类型?我宁愿不必根据文件扩展名对每种类型进行硬编码,但我可能需要这样做。

这是我的一点代码供参考

// 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;
            },  
        });
    }

File_x0020_Type 属性 of SP.ListItem resource 可用于该目的。

如果您的查询(似乎是您的情况)returns 文件 SP.FileCollection resource, 文件类型 可以这样检索:

/_api/web/getfolderbyserverrelativeurl('<folder url>')/Files?$select=ListItemAllFields/File_x0020_Type&$expand=ListItemAllFields

如果您的查询 returns list items SP.ListItemCollection resource, file type 可以像这样检索:

/_api/web/lists/getbytitle('<list title>')/items?$select=File_x0020_Type