Search result on bases of file name using Microsoft graph api - microsoft-graph-api

I am using Microsoft Graph API for OneDrive (personal and business) integration. I am able to get content using the Search API but it is not returning the results expected per the documentation.
I have also compared response using Microsoft Graph Explorer with same request.
I am using these api requests:
GET /me/drive/search(q='doc') when searching on root.
GET /me/drive/root:/{item-path}:/search(q='{search-text}') when searching on non-root folder
These calls are not returning all of the files I would expect. I am not able to files which are 0 byte (empty).

search not return specific type of files, like .png, .jpg,.md,.js, maybe even more. for my testing, it will return .pdf, docx, odp and xls

Related

Microsoft Graph API, Get SharePoint Site Admin/Members

Trying to use Microsoft's Graph API to iterate through Sharepoint sites, in order to find and remove a user's access to sites when they go out of the country.
Graph / Sharepoint API documentation: https://learn.microsoft.com/en-us/graph/api/resources/site?view=graph-rest-1.0
I get a list of all sites using the Get Sites endpoint:
https://graph.microsoft.com/v1.0/sites
I then filter out personal / OneDrive sites, and try to use the List Site Permissions endpoint to try to get admins/members: https://learn.microsoft.com/en-us/graph/api/site-list-permissions?view=graph-rest-beta&tabs=http
Unfortunately, I get an empty value array in response for all sites:
{'#odata.context': "https://graph.microsoft.com/v1.0/$metadata#sites('<domain>.sharepoint.com%asdfasdgsdf-ca8f-4ec1-bb3e-sdfsdfsdgsdf%2Cbed949c3-3f83-47c2-a263-sdfsdfsdgasdf')/permissions", 'value': []}
I know that the query is fine, because if I change it to instead hit the Site Lists endpoint, I get a response which includes various values:
{'#odata.context': "https://graph.microsoft.com/v1.0/$metadata#sites('<domain>.sharepoint.com%sdfsdfsdgsd-7dce-4564-9de8-eb2dfc02eb86%sdfsdgsdfsdf-ab8c-464f-a77d-sdfsdfsdgsdg')/lists", 'value': [{'#odata.etag': '"sfwsdgsdg-aff1-4295-8443-sdfsdgasdf,7"', 'createdDateTime': '2020-08-26T16:17:41Z', 'description': 'This library contains items that have been modified or deleted but must remain available due to eDiscovery holds. Items cannot be modified...
...
So, the Site ID I am passing to the permissions request must be correct. Is this the wrong endpoint then? Or, is this functionality missing from the Graph API, and my only solution is via the SP Powershell module?
I have tried with both the v1.0 and beta vesions of the API.
Thanks!
According this it looks like a bug in Graph API.

Access OneDrive personal vault through API

Microsoft introduced a special folder in OneDrive (I only see it in a personal OneDrive account, not in OneDrive for Business) called "Personal Vault". I searched the documentation of MS Graph API but could not find this mentioned.
So my question is: is there any way to access this personal vault as a third-party app?
Same problem here.
Funny thing is that:
It does not appear in the root children
It appears if you call the delta API where you can see a folder with the
specialFolder attribute not null and whose specialFolder.name is
"vault". This specialFolder, though, has the deleted facet, with the
state set to "hardDeleted". If you extract the ID from the delta API
call and try to address the folder directly with
https://graph.microsoft.com/v1.0/me/drive/items/{id}, however, you
get an access denied error

Does Microsoft Graph API /search() work when using app-only token

I am trying to use the graph API (v1.0) to search for files and folders within our team's sharepoint/onedrive folders but when using an app-only token I consistently get no results for any /search(...) request I try.
I have an registered and application with https://apps.dev.microsoft.com and have requested and granted consent for the permissions stated in the API Documentation;
User.ReadWrite.All, Group.Read.All, Sites.Read.All, Sites.ReadWrite.All, Group.ReadWrite.All, Sites.Manage.All, Files.ReadWrite.All, User.Read.All, Files.Read.All, Sites.FullControl.All
When I request a token from https://login.microsoftonline.com/ I can see the granted permissions in the payload.
Using this token I can successfully access the drive and retrieve information about its contents using drive/root
I have a small test folder structure that contains a few simple text files.
I can confirm that this token gives me access to my files and folders by calling drives/{driveid}/items/{folderid}/children recursively to build a complete tree structure.
However, when I try to search the drive for filenames or text phrases that I know exist I get no results at all, no matter what combination of paths or search terms I use. I've never seen a search result from an app-only token.
Examples that I've tried;
sites/root/drive/search(q='test')
drive/root/search(q='test')
drives/{driveid}/search(q='test')
drives/{driveid}/root/search(q='test')
The phrase "test" exists both as a filename and also within the text content of some of the files.
I get an HTTP:200 response but the value collection https://graph.microsoft.com/v1.0/$metadata#Collection(driveItem) is always empty.
I'm building http requests in my application (Coldfusion), not using a SDK.
I wondered if I was having encoding problems so have tried various approaches of url-encoding the search part of the url (q='test') etc.
I've also tried running the same requests with Postman and cUrl but the results are the same. Empty.
The only thing that works is when I try the same searches using the Graph Explorer I get the results I would expect to see.
I know that Explorer uses a different type of token so I'm wondering if this is and issues related to my token being app-only.
Does anyone know if .../search() actually work for app-only tokens?
Does anyone know if .../search() actually work for app-only tokens?
The answer is yes, ../search() actually work for app-only tokens
Based on my test, the following search API works well for app-only token
https://graph.microsoft.com/v1.0/drive/root/microsoft.graph.search(q='test')
https://graph.microsoft.com/v1.0/drives/{driveid}/root/microsoft.graph.search(q='test')
https://graph.microsoft.com/v1.0/drives/{driveid}/microsoft.graph.search(q='test')
https://graph.microsoft.com/v1.0/sites/root/drive/search(q='test')
For permission config, just put "Files.Read.All, Files.ReadWrite.All, Sites.Read.All, Sites.ReadWrite.All" into the GraphScopes in the project config file as docs states.
Above answer is based on your description:****register app in http://apps.dev.microsoft.com/. You can ignore the SDK although it is the best choice. If you don't have a project config with GraphScopes, you need to pass the scope to auth request, it is difficult for operation.
If your app-only mean register app in SharePoint online, then the Graph Search API won't work. SharePoint doesn't share AccessToken with Graph directly.
Here is the same question again. The answer has been commented by a microsoft developer. He confirmed, it was a bug.

OneDrive query returns empty array

I am currently working on a solution that is accessing OneDrive in Office 365 using Microsoft Graph. I am using the adal4j library to handle authentication and have configured the app in portal.azure.com.
My question relates the call to get the children for a specified drive. I am using a query similar to the one shown below, as I want to get folders and files at the root level of a specified users drive:
https://graph.microsoft.com/v1.0/users/*user id*/drives/*drive id*/root/children
When I login to the Graph Explorer and execute the query, I get a json result showing the root folder contents for the drive and user specified. All works as expected.
When I call it from my java application, the JSON node value is empty ([]).
Initially my thought was, because the Graph Explorer uses a different app id in the portal it was possibly something to do with access rights. However, I successfully read user profiles in our O365 tenant, the drive id's for each user, and if I execute the following:
https://graph.microsoft.com/v1.0/users/*user id*/drives/*drive id*/root/search(q='')
It provides me a complete list of all of the folders, sub folders etc within the appropriate user's drive.
Therefore, making me think this is a bug with the Graph query I am attempting to use rather than an authorization issue, but, that wouldn't explain why it works in the Graph Explorer.
The same java method is used for all calls, and the url is passed in as a parameter.
Just to follow up, the azure portal app permissions has the capability of adding permissions for the graph api. This was, indeed the problem. It would appear that the search was ignoring the permission and successfully reading the data whereas the /children call was honouring the security model. This caused a lot of confusion, but is now resolved.
Thanks Marc for your help.

How to get a temporary (or permanent) regular URL for a file in OneDrive using the Microsoft Graph API

Using createLink, for example with the POST parameters: {"type": "view", "scope": "anonymous"}, you get a response including a webUrl which will open the item (in read-only or read-write mode, depending on the POST parameters) in Office Online. Because of the 'anonymous' scope, anyone (no login required) can open the office online page.
Unfortunately, I don't a link to Office Online, and it looks like this is the functionality provided by Create a sharing link for a DriveItem.
I need a link to the actual file (to download it).
Something like many other 'files' APIs allow. This is, generate a time-limited (or permanent) URL to file.
Is this possible?
EDIT: Clarification: Download the contents of a DriveItem (i.e. a Stream) is not what I'm looking for.
There are four ways of linking to a file in OneDrive via Microsoft Graph:
The web preview for the file, which is accessed from the webUrl property of DriveItem. This requires the user to be signed in to access.
The WebDAV URL for the file, which is accessed from the webDavUrl property of DriveItem. This also requires the user to be signed in to access, but is a direct link to the file. Note: this is available via Microsoft Graph, but is only documented on dev.onedrive.com.
Creating a sharing link, which provides anonymous or organization restricted access to the web preview of the file.
Download link, which is a short-duration URL available to download the contents of the file programmatically.
It sounds like webDavUrl might be the link you are looking for.

Resources