Search code examples
sharepointmicrosoft-graph-apionedrive

How To Retrieve Custom Columns For DriveItems in MS Graph


I'm trying to use the Graph API to retrieve a hierarchy of files in a Sharepoint document library. Since document libraries are stored in "drives" (is it technically correct to call it OneDrive?), I'm using the /drives endpoint to fetch a list of files, like this:

https://graph.microsoft.com/beta/drives/{driveid}/root/children

I would like to get information from some of the custom columns that exist when viewing these items through Sharepoint. Using ?expand=fields doesn't work because fields only exists in listItem object of the /sites endpoint, not in the driveItem object of /drives endpoint. If I try obtaining the listItem from a single driveItem (traversing the Graph from OneDrive to Sharepoint), and then expanding the fields, like

https://graph.microsoft.com/beta/drives/{driveid}/items/{driveItemId}/listItem?expand=fields

this retrieves built-in columns (Author, DocIcon, and some others) but doesn't seem to retrieve the custom columns. I've also tried getting the list of files from the /sites endpoint, and using ?expand=fields will get the custom columns, but it gets every file from every subfolder, rather than the current folder path. But I feel that deserves its own SO question.

Is it possible to retrieve custom column information from driveItems?


Solution

  • I did some testing. What SHOULD work is:

    https://graph.microsoft.com/beta/drives/{driveid}/root/children?$select=id,MyCustomColumnName

    However, when I did that, it just returned that id field. In my opinion, that is a bug in the graph because this same type of query does work in the SharePoint REST api.

    If this helps, you can accomplish this by using the SharePoint REST api. Your endpoint query would be something like:

    https://{yoursite}.sharepoint.com/sites/{sitename}/_api/web/lists/(' {DocumentLibraryID}')/items?$select=id,MyCustomColumnName

    There are other ways to do the same query.