pnp / PnP-PowerShell

SharePoint PnP PowerShell CmdLets
https://pnp.github.io/powershell
Other
990 stars 663 forks source link

Get-PnPListItem is not returning the fieldvalues for lookup columns. #2968

Open saksham16 opened 3 years ago

saksham16 commented 3 years ago

Environment- SharePoint Online

Hi All,

I was given a task to built a PowerShell script which can be used to export the data of certain columns from certain lists from a SharePoint Subsite. I have built the script for it, however the issue I am facing with the command is that it is not able to pull the fieldvalues for the some lookup and metadata fields from certain list.

I have tried to query the fields by using the field parameter in the command which gets me the values for those fields, however if I am not querying any field then it doesn't get the fieldvalues for those fields. At first, I thought the library schema could be corrupted. So, I raised a premier ticket with Microsoft to find that out but they confirmed that the schema is not corrupted and is working fine, then I thought may be the columns schema is corrupted but then that also should not be corrupted as all the columns are defined in a site content type and for the same fields on a different list in the same subsite I am able to get the fieldvalues for them.

Let me know if anyone did face the same on their tenant and if anyone know why this is happening.

Regards, Saksham image

saksham16 commented 3 years ago

I know after looking at the screenshot it looks like that the data is not in the list but it is not the case as the field content owner does have a fieldvalue but it does not fetches the fieldvalues for the column in the first list as shown in the above screenshot.

Also, if I use the field parameter then it fetches the result as shown below: image

ToddKlindt commented 3 years ago

This is by design, and unfortunately it's very confusing. By default, Get-PnPListItem, and many other Get-PnP* cmdlets, don't retrieve every property for every object they get. This is done to reduce the payload coming back from Microsoft 365, which reduces the time these operations take. As you know the FieldValues property of a ListItem can be expanded and can hold countless properties. A large list, with a large schema could be huge and take a long time to respond. The PnP developers have decided to get a common set of properties instead of getting them all and risking a bad experience. You'll already discovered that you can specify the properties you want with the -Fields parameter of Get-PnPListItem. There is another cmdlet, Get-PnPProperty, that will retrieve those additional properties after you've done your initial Get. The Help for Get-PnPProperty shows a couple of examples of that. I've tripped over this a lot myself over the years. I will add some text to the Get-PnPListItem help that explains this and explains how to handle it.

saksham16 commented 3 years ago

Thank you so much @ToddKlindt for the reply.

I think you might be correct but it is happening with some lists only and lists in which I am facing this issue have less than 50 items in it and I even tried by creating a list using the template of a affected one, which has only 1 item right now and it does not fetches the fieldvalues for some lookup columns for that as well. Also, in production the list for which it is able to fetch the fieldvalues have more items then the the one I am facing issues with

I have tried Get-pnpproperty, however I would like to know how can I get the fieldvalues through that like you said in your Blog: "https://www.toddklindt.com/blog/Lists/Posts/Post.aspx?ID=851" .

ToddKlindt commented 3 years ago

Get-PnPListItem, and the other Get* cmdlets don't every retrieve all of the properties, regardless of how many objects are being returned. You always have to specify additional properties like FieldValues.
If you know you're going to need FieldValues then you should add -Fields FieldValues to your initial Get-PnPListItems command. Example #4 of help Get-PnPListItem -Examples shows how to do that. You could get them afterwards with something like$listitems | ForEach-Object { Get-PnPProperty -ClientObject $_ -Property FieldValues }