question

APIPointNewbie-3149 avatar image
0 Votes"
APIPointNewbie-3149 asked CarlZhao-MSFT commented

Exclude Files in Graph Search

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

dotnet-csharpmicrosoft-graph-sdkmicrosoft-graph-files
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Would you please provide us with an update on the status of your issue?

0 Votes 0 ·

1 Answer

Paul-5034 avatar image
0 Votes"
Paul-5034 answered Paul-5034 commented

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://docs.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://docs.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();
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello, thank you very much for your answer:

I tried to try this but got the following exception:
Status Code: NotImplemented

 Microsoft.Graph.ServiceException: Code: notSupported
    
 Message: Only createdDateTime is supported for filtering
    
 Inner error:
    
     AdditionalData:
    
     date: 2021-09-15T13:49:01
0 Votes 0 ·
Paul-5034 avatar image Paul-5034 APIPointNewbie-3149 ·

Ah, it looks like filtering by anything other than created date is unsupported for OneDrive for Business, SharePoint Online and SharePoint Server 2016. Their documentation states that only 'name' and 'url' are acceptable filters but that seems incorrect:
https://docs.microsoft.com/en-us/onedrive/developer/rest-api/concepts/filtering-results?view=odsp-graph-online#filterable-properties

That does somewhat kneecap that idea.

0 Votes 0 ·