msgraph-sdk-php icon indicating copy to clipboard operation
msgraph-sdk-php copied to clipboard

Support nested expansion deserialization - ex Read SharePoint List

Open Sam-Othman opened this issue 3 years ago • 7 comments

Hi team,

I am a bit of a rookie but keen to see if this is an issue or if I am doing something wrong. I have searched everywhere but can't seem to find a solution so sorry if this is the wrong place to log this.

I am trying to read the data from a SharePoint list. It is working within the API explorer but not when I move it into PHP. In the API explorer I am hitting the below endpoint:

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

When I do the below code in PHP it brings back the columns but the items are coming back in an empty array:

$list = $graph->createRequest("GET", "/sites/{site-id}/lists/{list-id}/?expand=columns,items(expand=columns)") ->setReturnType(Model\Site::class) ->execute();

Any help would be really appreciated!

Thanks

Sam AB#7976

Sam-Othman avatar Feb 03 '21 04:02 Sam-Othman

I have found that this also works in the API Explorer but unsure which class to use as it returns an empty array when using the Site class as per my last snippet:

/sites/{site-id}/lists/{list-id}/items?$expand=fields

Any help would be greatly appreciated.

Sam-Othman avatar Feb 03 '21 04:02 Sam-Othman

Hello @Sam-Othman

You need to return a List not a Site object.

$list = $graph->createCollectionRequest("GET", "/sites/{site-id}/lists/{list-id}/?expand=columns,items(expand=columns)")
->setReturnType(Model\List::class)
->execute();

Let me know if that helps. If it doesn't, please reopen this issue. Thank you!

MIchaelMainer avatar Feb 09 '21 20:02 MIchaelMainer

Hi @MIchaelMainer

Thanks for replying to me on this. I have tried to use the List Object however I am getting the below error:

PHP Parse error: syntax error, unexpected 'List' (T_LIST), expecting identifier (T_STRING) in D:\inetpub\wwroot\ithub-staging\resources\graph-get-list.php on line 11

I couldn't find a file named "List" within the Models folder, however I did find one called ListItems which I attempted and it ran successfully and populated the information regarding the list (column names and properties) however the 'Items' array was still empty. It works fine with the Graph Explorer so I believe I have the URL correct, I must just be doing something wrong when trying to do this via the SDK.

Hopefully this all makes sense!

Sam

Sam-Othman avatar Feb 09 '21 23:02 Sam-Othman

Hi @MIchaelMainer,

Any chance I can get this issue reopened and looked into?

Thanks!

Sam

Sam-Othman avatar Mar 11 '21 04:03 Sam-Othman

@Sam-Othman Thank you raising this some more. I just realized that we have to rename List to GraphList since List is keyword. Can you try the following:

$list = $graph->createCollectionRequest("GET", "/sites/{site-id}/lists/{list-id}/?expand=columns,items(expand=columns)")
->setReturnType(Model\GraphList::class)
->execute();

MIchaelMainer avatar Mar 11 '21 05:03 MIchaelMainer

@MIchaelMainer Thank you for reopening this. When I do the above, I still get the full array for [columns] but only the below for [items]

[[email protected]] => https://graph.microsoft.com/v1.0/$metadata#sites('91a780c5-5ffc-43dc-987f-f410ed137ed3')/lists('b05cc6af-9fb5-4223-9320-3d34bcb351e5')/items [items] => Array ( )

Sam-Othman avatar Mar 11 '21 05:03 Sam-Othman

I think what's happening is that we don't support the embedded expansion in deserializing our models.

https://github.com/microsoftgraph/msgraph-sdk-php/blob/2ff48a3492c7570ed65e5598ecea67d88cc0925e/src/Http/GraphResponse.php#L137

At this point, I think you'll need to either perform this in two calls like (I haven't tried this out and I'm not familiar with the endpoint):

$listwithcolumns = $graph->createCollectionRequest("GET", "/sites/{site-id}/lists/{list-id}/?expand=columns,items(expand=columns)")
                                 ->setReturnType(Model\GraphList::class)
                                 ->execute();

$listitemswith columns = $graph->createCollectionRequest("GET", "/sites/{site-id}/lists/{list-id}/items?expand=columns")
                                 ->setReturnType(Model\ListItem::class)
                                 ->execute();

Or

$response = $graph->createCollectionRequest("GET", "/sites/{site-id}/lists/{list-id}/?expand=columns,items(expand=columns)")
                                 ->execute();
$rawbody = $response->getRawBody();
/* Parse it out of the json */

MIchaelMainer avatar Mar 11 '21 06:03 MIchaelMainer