Sharepoint Online list items query returns empty array with Microsoft Graph API

Viewed 748

TLDR: I am trying to get the list items of a Sharepoint Online list with the Microsoft Graph API in Powershell. But all I get back is an empty array.

I have created an application in Azure and given it these permissions:

enter image description here

I know it's way too much permissions for what I need, but for the sake of troubleshooting and making sure it's not a permission issue, I added them all.

This is the code I use to get the Graph API token:

$TenantId = 'my-tenant-id'
$ClientId = 'my-client-id'
$ClientSecret = 'my-client-secret'

$Body = @{
    'tenant' = $TenantId
    'client_id' = $ClientId
    'scope' = 'https://graph.microsoft.com/.default'
    'client_secret' = $ClientSecret
    'grant_type' = 'client_credentials'
}

$Params = @{
    'Uri' = "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token"
    'Method' = 'Post'
    'Body' = $Body
    'ContentType' = 'application/x-www-form-urlencoded'
}

$AuthResponse = Invoke-RestMethod @Params

$Headers = @{
    'Authorization' = "Bearer $($AuthResponse.access_token)"
}

When I run the following command, just to get the list info, it works.

$SiteID = "my-site-id"
$ListID = "my-list-id"

$url = "https://graph.microsoft.com/v1.0/sites/$($SiteID)/lists/$($ListID)"

$Result = Invoke-RestMethod -Uri $url -Headers $Headers -Method Get -ContentType "application/json"

$Result | fl

This gives me the following result back:

@odata.context       : https://graph.microsoft.com/v1.0/$metadata#sites('abc.sharepoint.com%2Csite-id')/lists/$entity
@odata.etag          : "list-id,1139"
createdDateTime      : 2020-09-22T08:18:49Z
description          :
eTag                 : "list-id,1139"
id                   : list-id
lastModifiedDateTime : 2020-10-21T14:53:18Z
name                 : list-name
webUrl               : https://abc.sharepoint.com/sites/my-site/Lists/my-list
displayName          : my-list
createdBy            : @{user=}
lastModifiedBy       : @{user=}
parentReference      : @{siteId=abc.sharepoint.com,list-id
                       cca2b90}
list                 : @{contentTypesEnabled=True; hidden=False; template=genericList}

So I know my authentication with the Graph API works.

But then when I want to get the actual list items, I get an empty array back.

Command:

$SiteID = "my-site-id"
$ListID = "my-list-id"

$url = "https://graph.microsoft.com/v1.0/sites/$($SiteID)/lists/$($ListID)/items?expand=fields"

$Result = Invoke-RestMethod -Uri $url -Headers $Headers -Method Get -ContentType "application/json"

$Result | fl

Result:

@odata.context : https://graph.microsoft.com/v1.0/$metadata#sites('abc.sharepoint.com%2Csite-id')/lists('list-id')/items
value          : {}

I have no idea what the issue is. Similar posts on here made me think it's a permission problem. But I've thrown every permission in the book at it and it's still not returning the list items.

Help would be greatly appreciated.

1 Answers

Since you are using Microsoft Graph API, you need to select the microsoft graph api permissions in the Azure AD portal but not under the sharepoint. The permissions under the sharepoint are related to sharepoint API.

Related