microsoft-graph-apisharepoint-online

Expand DriveItem's listItem property while selecting fields from listItem


I've been using Graph for quite some time now and it seems I have found an inconsistent behavior (or gap/bug).

As I described in my answer to a previous post here, the below graph query works.

https://graph.microsoft.com/v1.0/drives/{{drive-id}}/items/{{folder-id}}/children?$expand=listItem

and it returns something like:

{
    "@odata.context": "...",
    "@odata.nextLink": "...":,
    "@microsoft.graph.tips": "Use $select to choose only the properties your app needs, as this can lead to performance improvements. For example: GET drives('<key>')/items('<key>')/children?$select=audio,bundle",
    "value": [
       {
            "@microsoft.graph.downloadUrl": "...",
            "createdDateTime": "2024-08-05T17:36:38Z",
            "eTag": "\"{....},1\"",
            "id": "....",
            "createdBy: "{...}",
            "lastModifiledBy: "{...}",
            "parentReference": {
                "driveType": "documentLibrary",
                "driveId": "...",
                "id": "...",
                "name": "1213458",
                "path": "/drives/.../root:/1213458",
                "siteId": "..."
            },
            "file": {
                "mimeType": "application/vnd.ms-outlook",
                "hashes": {
                    "quickXorHash": "X3HpXmd2MnjSz+sx2rdXQMlxQhQ="
                }
            },
            "fileSystemInfo": {
                "createdDateTime": "2024-08-05T17:36:38Z",
                "lastModifiedDateTime": "2024-08-05T17:36:38Z"
            },
            "shared": {
                "scope": "users"
            },
            "listItem@odata.context": "...",
            "listItem": {
                "@odata.etag": "\"...,1\"",
                "createdDateTime": "2024-08-05T17:36:38Z",
                "eTag": "\"...,1\"",
                "id": "109",
                "lastModifiedDateTime": "2024-08-05T17:36:38Z",
                "webUrl": "...",
                "createdBy": {...},
                "lastModifiedBy": {...},
                "parentReference": {
                    "id": "...",
                    "siteId": "..."
                },
                "contentType": {...},
                "fields@odata.context": "https://graph.microsoft.com/v1.0/.../listItem/fields/$entity",
                "fields": {
                    **{{ fields and custom fields here }}**
                }
            }
        },
       ...

Now, if I try to trim the properties returned to return just driveItem.id, driveItem.listItem.id, and driveItem.listItem.fields, I get this:

Graph query:

https://graph.microsoft.com/v1.0/drives/{{drive-id}}/items/{{folder-id}}/children?$expand=listItem($select=id,fields)&$select=id,listItem

And the results:

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives('b%21QLkUIRN41EGxyL2ze-FcYpwt-281sa5Oj2fmTfCb89X6zP6Qd-aaR79wRgAD_pwn')/items('01IHAJKCDCVMH3EXQARVCI5PEFK7TEKYFL')/children(id,listItem,listItem(id,fields))",
    "@odata.nextLink": "https://graph.microsoft.com/v1.0/drives/b!QLkUIRN41EGxyL2ze-FcYpwt-281sa5Oj2fmTfCb89X6zP6Qd-aaR79wRgAD_pwn/items/01IHAJKCDCVMH3EXQARVCI5PEFK7TEKYFL/children?$expand=listItem(%24select%3did%2cfields)&$select=id%2clistItem&$skiptoken=UGFnZWQ9VFJVRSZwX0ZpbGVMZWFmUmVmPTk2MjU3OTk0X0xvc3MrUnVuK1JlcG9ydCstK0F1ZGF4Lm1zZyZwX0lEPTQ4Nw",
    "value": [
        {
            "@odata.etag": "\"{FF50F7F6-DC36-43D1-B952-79272B3C4F2B},1\"",
            "id": "01IHAJKCHW65IP6NW42FB3SUTZE4VTYTZL",
            "listItem@odata.context": "https://graph.microsoft.com/v1.0/.../listItem(id,fields)/$entity",
            "listItem": {
                "@odata.etag": "\"...,1\"",
                "id": "109"
            }
        },
        ...

As you can see, the fields property is not included in the results.

Similarly, this works just fine:

Graph query:

https://graph.microsoft.com/v1.0/sites/{{site-id}}/lists/{{list-id}}/items?$expand=fields($select=id,FileLeafRef)&$select=id,fields

Here are the results, which include the fields property (hence, inconsistent with the previous result):

{
    "@odata.context": "https://graph.microsoft.com/v1.0/.../items(id,fields,fields(id,FileLeafRef))",
    "@odata.nextLink": "...",
    "value": [
        {
            "@odata.etag": "\"...,3\"",
            "id": "1",
            "fields@odata.context": "https://graph.microsoft.com/v1.0/.../fields(id,FileLeafRef)/$entity",
            "fields": {
                "@odata.etag": "\"caf3311d-1de9-4a89-b2a0-5aaa9ac93de0,3\"",
                "id": "1",
                "FileLeafRef": "..."
            }
        },
        {
            "@odata.etag": "\"...,1\"",
            "id": "2",
            "fields@odata.context": "https://graph.microsoft.com/v1.0/.../fields(id,FileLeafRef)/$entity",
            "fields": {
                "@odata.etag": "\"...,1\"",
                "id": "2",
                "FileLeafRef": "..."
            }
        },
        ...

I know there's some mixing of listItem and driveItem types and properties involved in the first part.

My questions is:

Is there a way of querying Graph to:

???


Solution

  • You need to expand fields inside the listItem, because fields is a relationship, not a regular property of the listItem resource.

    GET https://graph.microsoft.com/v1.0/drives/{drive_id}/items/{drive_item_id}?&expand=listitem($select=id;$expand=fields)&$select=id,listItem
    

    To return only specific fields, you can use nested $select

    GET https://graph.microsoft.com/v1.0/drives/{drive_id}/items/{drive_item_id}?&expand=listitem($select=id;$expand=fields($select=id,FileLeafRef))&$select=id,listItem