How to get file id and file name from one drive account

Amol Harde 25 Reputation points
2023-03-09T11:00:59.8633333+00:00

Hi All,

Please help me for get file id and file name from one drive using java.

If someone have any code please provide us.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,499 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
863 questions
{count} votes

1 answer

Sort by: Most helpful
  1. HarmeetSingh7172 4,811 Reputation points
    2023-03-10T02:03:00.4933333+00:00

    Hello Amol Harde,

    Thanks for reaching out!

    To list user drives:

    GET https://graph.microsoft.com/v1.0/users/48d31887-5fad-4d73-a9f5-3c356e68a038/drives

    Java Code Snippet:

    GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
    
    DriveCollectionPage drives = graphClient.users("48d31887-5fad-4d73-a9f5-3c356e68a038").drives()
    	.buildRequest()
    	.get();
    
    

    To list children in the root of the current user's drive: This will list all the items (folders and files) along with the Item-IDs present in drive at root level.

    GET /me/drive/root/children

    Java Code Snippet:

    GraphServiceClient graphClient = new GraphServiceClient( authProvider );
    
    var children = await graphClient.Me.Drive.Root.Children
    	.Request()
    	.GetAsync();
    
    

    To list children of a DriveItem with a known ID: This will list all the sub-items (folders and files) along with the Item-IDs inside a driveItem (folder).

    Please note that every item-id is either a file ID or folder ID. The response will have id, name of items and other details. You can identify whether an item is a folder or a file by looking for "files" or "folder" property for that specific Item in response. Refer drive Items resource.

    GET /drives/{drive-id}/items/{item-id}/children

    Java Code Snippet:

    GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
    
    DriveItemCollectionPage children = graphClient.drives("{drive-id}").items("{item-id}").children()
    	.buildRequest()
    	.get();
    
    

    References: List drives, List children of a driveItem

    Hope this helps.

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

    0 comments No comments