sharepointmicrosoft-graph-apisharepoint-search

How to search sharepoint file across document library


I'm trying to search files in SharePoint with GET https://graph.microsoft.com/v1.0/sites/root/drive/root/search(q='text file') but it searches only in the root site. Is there any way to search across the document library using graph API?

Something similar to POST https://graph.microsoft.com/v1.0/search/query. This endpoint searches files across the document library.

Is it possible to implement the POST api call behavior in the GET api call? Why because the response from POST api call doesn't provide information about file-mimeType, so I need to switch to GET api call.


Solution

  • You can specify the fields you want back in the response, as part of the fields sub-property. Specify mimeType.

    To retrieve a custom property for a driveItem, query listItem instead.

    POST https://graph.microsoft.com/v1.0/search/query
    

    Request body

    {
        "requests": [
            {
                "entityTypes": [
                    "listItem"
                ],
                "query": {
                    "queryString": "text file"
                    // filter by contentClass
                    //"queryString": "text file contentclass:STS_ListItem_DocumentLibrary"
                },
                "fields": [
                    "id",
                    "fileName",
                    "contentClass",
                    "createdDateTime",
                    "lastModifiedDateTime",
                    "webUrl",
                    "sharepointIds",
                    "createdBy",
                    "modifiedBy",
                    "parentReference",
                    "mimeType",
                    "fileType"
                ]
            }
        ]
    }
    

    Specify select properties