Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 813
Adam Dunn Avatar asked Dec 24 '22 17:12

Adam Dunn


2 Answers

I spent a lot of time digging around with the different syntax possibilities and was finally able to get custom library properties using this query format. This is the only one that has produced my custom/user-defined fields for a document library.

https://graph.microsoft.com/v1.0/drives/insert_drive_id_here/root/children?expand=listItem

Shortened result:

{
"@odata.context": "...",
"value": [
        {
        "@microsoft.graph.downloadUrl": "...",
        "[email protected]": "...",
        "listItem": {
            "@odata.etag": "...",
            "[email protected]": "...",
            "fields": {
                "@odata.etag": "...",
                "Title": "...",
                "Other_Custom_Property": "..."
                }
            }
        }
    ]
}
like image 77
Greg.Timm Avatar answered Jan 15 '23 18:01

Greg.Timm


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.

like image 42
Gregg Lestina Avatar answered Jan 15 '23 18:01

Gregg Lestina