Exclude Files in Graph Search

APIPointNewbie 146 Reputation points
2021-09-14T14:04:14.29+00:00

Hello best community in the world ;-)

I have a question again :-)

You can easily search for data items with the MS Graph SDK.
C# Example:

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var search = await graphClient.Me.Drive.Root
    .Search("Contoso Project")
    .Request()
    .GetAsync()

But how do I search only for folders, because files can also be called like folders and I want to exclude files in a search.

Is there a way to do this with the Graph SDK ?

About an answer I would be very happy.

Greetings

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,557 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,226 questions
{count} votes

Accepted answer
  1. P a u l 10,406 Reputation points
    2021-09-14T16:12:17.257+00:00

    There might be a more elegant way to do this, but this looks like it works:

    https://graph.microsoft.com/v1.0/me/drive/root/search(q='a')?filter=folder ne null

    For reference:
    This uses the "filter" parameter:
    https://learn.microsoft.com/en-us/graph/query-parameters#filter-parameter

    "DriveItem" has a "folder" property that's only present for folders, so that just checks if "folder" ne (not-equals) null:
    https://learn.microsoft.com/en-us/graph/api/resources/driveitem?view=graph-rest-1.0#properties

    I've not used the MS graph SDK myself but I believe your code should just be:

    GraphServiceClient graphClient = new GraphServiceClient( authProvider );  
          
    var search = await graphClient.Me.Drive.Root  
        .Search("Contoso Project")  
        .Request()  
        .Filter("folder ne null")  
        .GetAsync();  
    

0 additional answers

Sort by: Most helpful