Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search Document Library by filename using GRAPH API

I'm having a hard time figuring out how to query my Document Library in SharePoint. It just seems to return random files.

I need to search a Document Library by filename and return the files found plus a few custom columns, lets call them ColumnA and ColumnB.

My Document Library

enter image description here

I should mention that my site is called test site and my library is called test.

I've tried:

/v1.0/drives/{drive-id}/search(q='name:test')

Returns:
12345
54321
/v1.0/drives/{drive-id}/search(q='test')

Returns:
all files
/v1.0/drives/{drive-id}/search(q='name:dummy')

Returns:
none
/v1.0/drives/{drive-id}/search(q='dummy')

Returns:
dummy

Okay... so search doesn't do what I want as I cannot limit it to only search in the name property. Also I cannot $expand=fields as that'll result in Parsing Select and Expand failed. and I need to include ColumnA and ColumnB in the result.

So I tried

/v1.0/sites/{site-id}/lists/{list-id}/items?$expand=fields&$filter=startswith(fields/LinkFilename, 'test')

Returns
Field 'LinkFilename' cannot be referenced in filter or orderby as it is not indexed.

I do not want to create indexes nor do I want to use the unsafe Prefer: HonorNonIndexedQueriesWarningMayFailRandomly header.

So I tried

/v1.0/sites/{site-id}/lists/{list-id}/items?$expand=fields,driveItem&$filter=startswith(driveItem/name, 'test')

Returns
An unspecified error has occurred.

Any suggestions?

like image 272
Snæbjørn Avatar asked Apr 06 '26 23:04

Snæbjørn


1 Answers

You can apply the filter directly to the DriveItems:

v1.0/sites/siteId/drives/driveId/root/children?$filter=startswith(name, 'test')

Since the DriveItems are exposed as ListItems in SharePoint document libraries, you can expand the ListItem property of the DriveItems:

v1.0/sites/siteId/drives/driveId/root/children?$filter=startswith(name, 'test')&$expand=listitem

I am not quite sure, what kind of data you need from the ListItem, but you can also expand the properties of the ListItem:

v1.0/sites/siteId/drives/driveId/root/children?$filter=startswith(name, 'test')&$expand=listitem($expand=fields)
like image 127
Marc Avatar answered Apr 08 '26 17:04

Marc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!