Get Drive Item and List Item by File Name, Path and Guid using Graph API

You might have already known the resources ListItem and File if you have worked with SharePoint files with CSOM. In Graph API, the sharepoint files are represented by another resource DriveItem. The driveItem resource represents a file, folder, or other item stored in a drive. All file system objects in OneDrive and SharePoint are returned as driveItem resources. You can retrieve driveItem resource by following ways.

Get by driveItem unique identifier

#From SharePoint Online Site
https://graph.microsoft.com/v1.0/sites/{siteId}/drive/items/{itemId}
#From Office 365 Group associated SPO Site
https://graph.microsoft.com/v1.0/groups/{groupId}/drive/items/{itemId}
#From User's OneDrive
https://graph.microsoft.com/v1.0/me/drive/items/{item-id}

By file name

The below request queries the given file name in root folder alone. If you want to query in sub folder, then you have to provide your folder name in the api url (Refer below example)

#With root drive - Default document library
https://graph.microsoft.com/v1.0/sites/{siteId}/drive/root:/TestFile.txt
 
https://graph.microsoft.com/v1.0/groups/{groupId}/drive/root:/TestFile.txt

#With known drive id
https://graph.microsoft.com/v1.0/sites/{siteId}/drives/{drive-id}/root:/TestFile.txt

By file system path

The below request queries the given file name in TestFolder.

 #With root drive - Default document library
https://graph.microsoft.com/v1.0/sites/{siteId}/drive/root:/TestFolder/TestFile.txt

#With known drive id
https://graph.microsoft.com/v1.0/sites/{siteId}/drives/{drive-id}/root:/TestFolder/TestFile.txt

You can refer this post for more details : Get a DriveItem resource

Get ListItem from DriveItem resource

The driveItem resource includes the extended relationship with ListItem. The below graph api request retrieves the associated document library list item for the corresponding driveItem.

https://graph.microsoft.com/v1.0/sites/{siteId}/drive/items/{itemId}/listItem

Get DriveItem by File ID (Guid) using Graph API

Using above methods, you can get driveItem resource by file name, file path and drive item id. There may be a requirement to get this driveItem by file’s actual guid. This guid will be used with document URL in some cases. Actually I was in the same need with below file URL.

"https://MySPOTenant.sharepoint.com/sites/MySite/_layouts/15/Doc.aspx?sourcedoc={52173197-354A-532K-96BB-D4A5CDA8AF15}&file=TestFile.xlsx&action=default&mobileredirect=true"

Here my need was to find driveItem resource by using its document ID which is available in the document url. We can’t able to use this guid with the above endpoint (ex: sites/{siteId}/drive/items/{itemId}), because it supports only drive item unique id. So here my alternative approach is Search endpoint, we can provide this guid in search query and get the associated driveItem resource.

https://graph.microsoft.com/v1.0/sites/{siteId}/drive/root/search(q='52173197-354A-532K-96BB-D4A5CDA8AF15')

Actually searching by file guid will not provide perfect result in some cases, because it searches entire file system metadata, so you may get wrong result if the same guid text available as name of another file. As a work-around, once we get driveItem resource using above request, we can get the associated document library list item by providing driveItem id. The listItem resource includes the property eTag which includes the file guid, you can compare this eTag with file guid to ensure that you have found the correct resource. You can also cross-check the file name to get better result.

#Get the itemId from above search result items
https://graph.microsoft.com/v1.0/sites/{siteId}/drive/items/{itemId}/listItem

#ListItem result snippet 
"@odata.etag": ""52173197-354A-532K-96BB-D4A5CDA8AF15,3"",
"createdDateTime": "2019-07-02T12:11:44Z",
"eTag": ""52173197-354A-532K-96BB-D4A5CDA8AF15,3"",

Note : I am just sharing this method as a work-around, if you find any mistake or better method, then please post the same in below comments section.


Advertisement

2 thoughts on “Get Drive Item and List Item by File Name, Path and Guid using Graph API”

  1. What is the command to get a list of all files under “General” for a folder called something like, “Files”. Similar to the Powershell command “$filelist = Get-ChildItem -Path “\\server\path\Files” -Recurse

    Reply
    • I hope there is no way to get all files recursively and we can get only first-level files and folders using the following API:

      https://learn.microsoft.com/en-us/graph/api/driveitem-list-children

      You can try to use search method instead of children to get files from all the levels:

      Example 1: The following query retrieves all files and folders in the root scope

      https://graph.microsoft.com/v1.0/me/drive/root/search(q='')?$select=name,id,parentReference

      #With site root drive - Default document library
      https://graph.microsoft.com/v1.0/sites/{siteId}/drive/root/search(q='')?$select=name,id,parentReference

      Example 2: To search in the folder named ‘General’ located in root folder

      https://graph.microsoft.com/v1.0/me/drive/root:/General:/search(q='')?$select=name,id,parentReference

      #With site root drive - Default document library
      https://graph.microsoft.com/v1.0/sites/{siteId}/drive/root:/General:/search(q='')?$select=name,id,parentReference

      Check this post: https://stackoverflow.com/a/60471378

      Reply

Leave a Comment