SharePoint REST API Document Library - filter by folder name
-
19-01-2021 - |
Frage
I'm running a JavaScript script that updates permissions on folders in lists and document libraries. To do this I need to get the ID of the item I want to update. The folders all have unique names so I want to filter by the folder name. For list items this is relatively easy:
siteUrl + "/api/Web/Lists/getByTitle(' + listName + ')/items?$filter=Title eq '" + folderName + "'";
However for document libraries I am having trouble. The name of the folder isn't stored in the title field. I can't find the right way to filter the document library so that the only items it returns are the items I need to update. I have tried the following (neither work):
siteUrl + "/api/Web/Lists/getByTitle(' + listName + ')/items?$filter=Folder/Name eq '" + folderName + "'";
siteUrl + "/api/Web/Lists/getByTitle(' + listName + ')/items?$filter=BaseName eq '" + folderName + "'";
Does anyone know whether it is possible to filter based on the name of a folder?
My backup is to request all items and then loop over the items to find the right one.
Lösung
The name of the folder is stored in the Name field i.e FileLeafRef field value.
Also, to optimize the request, you should specify only the fields that you want using the $select
param.
So, you can filter based on the value stored in it and create your REST endpoint as :
siteUrl + "/api/Web/Lists/getByTitle(' + listName + ')/items?
$select=ID&$filter=FileLeafRef eq '" + folderName + "'";
Andere Tipps
If you want to do it only for one folder you can also use the url
siteUrl +"/_api/Web/GetFolderByServerRelativeUrl('/sites/MySite/MyLib/MyFolder')";