I’ve recently been working with a customer migrating some content from one SharePoint Online Site Collection to another. They have a lot of content, so automating the migrations with PowerShell was really the only we’d be able to get all of their content migrated before we died of old age. Part of what they wanted migrated over was the custom views they had in their Document Libraries. A couple of extra columns here, a grouping there, nothing too complicated. I fired up PowerShell (really VS Code) and started noodling.
Whenever I’m trying something new in PowerShell I have a standard process. I do a Get-Command to see if there’s a cmdlet that does what I need. In this case there were four cmdlets that piqued my interest, Add-PnPView, Get-PnPView, Remove-PnPView, and Set-PnPView. The process seemed super easy, do a Get-PnPView on the source View, gobble up all the details, then spit them back into Add-PnPView on the other site collection. Easy Peasy. Until it isn’t.
Here’s what the view looked like, roughly.
I didn’t do much to it. I added a couple of columns, Created By and File Size and set the view to group by Created By. Pretty standard stuff. Here’s what Get-PnPView told me about said view:
I can see the columns I added in the ViewFields property, but there’s no sign of the grouping anywhere. Knowing what I know about SharePoint I know that information is stored in a Query, but the ViewQuery property is conspicuously empty. What gives? The PnP, in an effort to be efficient, doesn’t download all of the property values for the objects we get. It downloads the schema, and then a subset of the properties that it thinks you’re most likely to use. What’s a fella to do if the thing you need is not in the PnP’s favorite list of properties? You break out one of the PnP’s overlooked gems, Get-PnPProperty.
Here’s the code for it all:
Connect-PnPOnline -Url https://toddklindt.sharepoint.com/sites/8884aced -Credentials Me
Get-PnPView -List Documents
Get-PnPView -List Documents -Identity 3c4126aa-d2fe-4b57-9a70-e03ebb9c76ef
$view = Get-PnPView -List Documents -Identity 3c4126aa-d2fe-4b57-9a70-e03ebb9c76ef
$view
$view | select *
$view.ViewQuery
Get-PnPProperty -ClientObject $view -Property ViewQuery
$view.ViewQuery
$view
$view | select *
Not only does Get-PnPProperty get the value of the Property, but it also populates the property in the variable. If you’re getting a collection of objects and need to pull one property for each of them it looks like this:
Get-PnPView –List Documents
$viewlist| ForEach-Object { Get-PnPProperty -ClientObject $_ -Property ViewQuery, ListViewXml }
That hydrates the ViewQuery and ListViewXml property for all of the objects in $viewlist.
It snuck some other properties in too, but I don’t mind.
In this case I was using the View object, but this method is necessary for any PnP object. In the past I’ve had to use it with PnPFile and PnPLists. Sometimes the PnP gives you an error message telling you you need to get the property value, sometimes it’s just empty and you have to know. Either way, it’s worth trying to get the property’s value with Get-PnPProperty.
tk
ShortURL: https://www.toddklindt.com/PoshGetPNPProperty