Graph API Search for DriveItems within a drive does not return image files if I specify itemId

Rei 31 Reputation points
2022-01-19T21:40:01.697+00:00

I'm using graph (Graph Explorer as well as my custom Azure Application which utilizes Graph API with the appropriate grants of Files.ReadWrite.All, Sites.ReadWrite.All) to search for specific files within a folder in a drive as documented here: https://learn.microsoft.com/en-us/graph/api/driveitem-search?view=graph-rest-1.0&tabs=http.

If I specify as follows it works:
/drives/{drive-id}/root/search(q='{search-text}')

However, if I specify the parent folder's Id, it returns empty or misses files (it seems it misses mostly png, jpg, adobe files):
/drives/{drive-id}/items/{folder-id}/search(q='{search-text}')

{  
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#Collection(driveItem)",  
    "value": []  
}  

This is also true when I use the https://graph.microsoft.com/v1.0/search/query endpoint.
If I specify a path, it will not return results.

            "query": {  
                "queryString": "myfile path:\"https://contoso.sharepoint.com/sites/Library/Shared Documents/PATH\""  
            }  

But if specify no path, or the Document library level, it will return the image files.

            "query": {  
                "queryString": "myfile path:\"https://contoso.sharepoint.com/sites/Library/Shared Documents\""  
            }  

Funny thing is that it usually does actually successfully bring up text or PDF documents, but images like PNG and JPG often get lost. That is confusing to me as well. However if I do the drive root search, those images can be found. Is there a reason why when I specify the parent folder the graph api doesn't work properly? I've given it some time to index, but doesn't seem to change. Any help would be appreciated.

I also noticed that the webUrl on an image file is something like:
https://contoso.sharepoint.com/sites/LIBRARY/Shared Documents/Forms/DispForm.aspx?ID=2702

but a docx file is
https://contoso.sharepoint.com/sites/LIBRARY/Shared Documents/My Path/myfile.docx

I would appreciate any help on this.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,715 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,686 questions
0 comments No comments
{count} vote

Accepted answer
  1. Srinivasa Rao Darna 6,696 Reputation points Microsoft Vendor
    2022-01-21T12:40:36.197+00:00

    Hi @Rei ,

    From Search for DriveItems within a drive, I don't see any reference example for /drives/{drive-id}/items/{folder-id}/search(q='{search-text}') for test this scenario.

    Using Search API, including ParentLink:/FolderName, along with Path should be able to retrieve files and images as well from a parent folder within a document library.

    POST https://graph.microsoft.com/v1.0/search/query  
    {  
    "requests": [  
        {  
            "entityTypes": [  
                "driveItem"  
            ],  
            "query": {  
                "queryString": "searchstring AND Path:https://contoso.sharepoint.com/sites/Library/Shared Documents AND ParentLink:/FolderName"  
            }  
        }  
    ]  
    }  
    

    Hope this helps.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have further questions about this answer, please click "Comment".

    3 people found this answer helpful.

0 additional answers

Sort by: Most helpful