3

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.

  • What if you call search on drive not root? GET https://graph.microsoft.com/v1.0/sites/root/drive/search(q='text file') – user2250152 Sep 10 '21 at 07:54
  • Nope. Even this doesn't work it behaves the same. I have many sites but this doesn't search files from other sites. @user2250152 – WSI Appfactory Sep 13 '21 at 05:49

1 Answers1

2

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

user2250152
  • 14,658
  • 4
  • 33
  • 57
  • Cool Thanks @user2250152 . This makes sense but `name` is missing in this response can't we pull the name of the file/folder using `listItem`? And by any chance can we restrict the OneDrive files in this search and search only SharePoint files. – WSI Appfactory Sep 13 '21 at 09:39
  • @WSIAppfactory Updated the answer and added "fileName" field. I'm not sure if you can filter only SharePoint files. Try to add field contentClass and try to filter by contentClass – user2250152 Sep 13 '21 at 09:58
  • The `webUrl` returned from the API call looks like `downloadUrl` & it downloads the specific file/document. Is there any way to fetch the `previewUrl` (URL to open the specific document/file)? – WSI Appfactory Sep 14 '21 at 11:33