TransWikia.com

Microsoft Graph/Sharepoint API search

SharePoint Asked by elb98rm on October 25, 2021

The Graph API isn’t well documented about a few things, and so I was wondering if anyone had some insight, or direct answers to the following problem:

Background

I am attempting to create a RESTful API that interacts with the shared documents of a sharepoint site to automate much of the process.

I will be storing customer documents in a user folders. These folders are created using an identity (id/guid) style naming convention. Each file they create or are allocated are stored there. There may be duplicate names across customers.

I have created the C & D of CRUD, and with a microsoft ID can easily R (and download).

However, there are problems searching, and the microsoft documentation is far from complete.

Currently confirmed used routes

These are the routes I’m (successfully) using to do my operations:

(all with the prefix: https://graph.microsoft.com/v1.0/)

  • Root list – Get: {site_name}/drive/root/children
  • Create – PUT: {site_name}/drive/items/{parent_folder_id}:/content
  • Retrieve – GET: {site_name}/drive/items/{resource_id}/
  • Download : GET: {site_name}/drive/items/{resource_id}/content
  • Delete : DELETE: {site_name}/drive/items/{resource_id}

These are working as expected.

TLDR; The Specific questions:

How do I format request in order to search:

  1. view files in a folder
  2. limited to one folder
  3. limited to a search criteria

Question detail #1

There is no documentation I can find that shows to perform the list, but .. not at root level!

  • Root list – Get: {site_name}/drive/root/children

.. so what is the format to limit this search to a folder.
Here’s some pseudo-code that doesn’t work:

  • Pseudo – Get: {site_name}/drive/{folder_name}/children
  • Pseudo – Get: {site_name}/drive/root/{folder_name}/children

Note: This is for the very common customer use case:

  • “Get all files owned by Joe Bloggs”

Question detail #2

Given that many files in different folders will have the same name, how do I format a search to be limited to a folder.
The current suggested search criteria is as follows:

  • /search(q='{search_value')

If I run the following query, it runs at a root level:

  • Get: {site_name}/drive/root/children?search(q='{search_value')

How do I format this to be limited to a folder?

Question detail #3

Is there a way to limit the search criteria to search by a property? For example, to distinguish between the following:

  • File 1 Filename : abcdff.jpg
  • File 2 ID : abcdef ...

…or some such combination of metadata.

So using some psuedo-SQL type URL:

  • Get: {site_name}/drive/root/children?search(q='{name=search_value}')

Thanks in advance to all.

One Answer

I'm just answering question detail #1.

As far as I've learnt, a SharePoint site has a drive associated with it and each document library or list has its own drive too.

In order to get the drive's id associated with a site or list/library, you've got to use the ODATA parameter expand:

GET sites/{site-id}/?expand=drive to get a site's drive id

GET sites/{site-id}/lists/{list-id}?expand=drive to get a list or library's drive id

NOTE: it doesn't work with items in a list. Don't ask me why...

Once you've got the drive id you need, you can access the root of that document library like:

GET drives/{doc-library-drive-id}/root/children: list the children of the root document library, where you'll see the first level of folders or document sets (very useful to use as "folders with metadata" and metadata inheritance.

You'll see that each folder has its own drive id too. So, to get the folder's children, I recommend using the following endpoint:

GET drives/{doc-library-drive-id}/items/{folder-drive-id}/children This will fetch the children of that folder. I use this endpoint because I've had problems using the {site_name}/drive/root/{folder_name}/children you suggested because of certain chars the folders may have (we speak Spanish in our organization).

NOTE: if you use the approach /sites/{site-id}/drive/items/{item-id}/children as described in the docs, you'll access the default document library of the site (Documents). So, if you need to get to a different document library, you'll need to specify its drive id like described above

So, my approach to get the folder's children is:

  1. Get the list's drive id as described above
  2. Access the library's root and get the id of the folder you want
  3. Use the folder's drive id to access its children

Answered by Andrés Biarge on October 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP