1
votes

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?

1

1 Answers

1
votes

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)