2

I have a rest api right now that accesses files in a folder. It works and returns the name of the file and a count accurately. I am trying to get data from a column field as well but everything I've done returns as undefined. I have my code cycling through files in the folder and all I need is to get the FileType field for each item.

"/_api/web/GetFolderByServerRelativeUrl('" + folderUrl + "')?$expand=Folders,Files&$select=fileType",
2
  • Is fileType a custom column that you added, or a default column? Jul 3, 2019 at 18:57
  • custom column that is a choice field
    – spUser01
    Jul 3, 2019 at 19:33

1 Answer 1

3

So, you are trying to do two things at once:

  • Get the Item Count for the given folder
  • Get the 'fileType' column for all items in the folder

This seems like a job better suited to multiple sequential REST calls, but this worked for me:

<server>/_api/web/GetFolderByServerRelativeUrl('/Pages')?$expand=Folders,Files/ListItemAllFields/FieldValuesAsText&$select=ItemCount,Folders/ItemCount,Folders/Name,Files/ListItemAllFields/FieldValuesAsText/File_x005f_x0020_x005f_Type,Files/ListItemAllFields/FieldValuesAsText/fileLeafRef

Here I'm doing multiple levels of expansion and selection:

$expand=Folders,Files/ListItemAllFields/FieldValuesAsText

  • The additional expansion on Files was just to get to the File_x005f_x0020_x005f_Type value for each file, which is the same as the file extension

$select=ItemCount,Folders/ItemCount,Folders/Name,Files/ListItemAllFields/FieldValuesAsText/File_x005f_x0020_x005f_Type,Files/ListItemAllFields/FieldValuesAsText/fileLeafRef

  • This selects ItemCount for the current folder (the folder you specified in GetFolderByServerRelativeUrl), ItemCount and Name for each sub-folder, and File_x005f_x0020_x005f_Type and fileLeafRef (the literal file name) for all files contained in the current folder, regardless of sub-folders.

Since this query goes several levels deep, it returns a lot of metadata that you probably don't need. So I would recommend using the odata=nometadata headers when making your REST call:

Accept:application/json;odata=nometadata Content-Type:application/json;odata=nometadata

Edit: If I recall correctly, the nometadata property only works with SP 2016+

3
  • still seems to be returning undefined, the column name in Sharepoint is FileType, Do i need 'x005f_x0020_x005f' ?
    – spUser01
    Jul 3, 2019 at 20:05
  • I'm not sure. Try running the query without the $select clause. It should return the full set of columns for each file, then you can find the ones you need. Also, if you're not already, I would suggest using a REST app like Postman* to explore the API. *I have no affiliation, it's just the one I use. Jul 4, 2019 at 2:29
  • running without the select clause worked! thanks!
    – spUser01
    Jul 8, 2019 at 23:00

Your Answer

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

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