SOLVED

Creating a View in SharePoint Online using PowerShell - using ViewScope

New Contributor

Hello, I am a SharePoint administrator for my organisation and I need some assistance in creating views using PowerShell. I can create the basic view, which is very straightforward and simply filters on the surname of the files in the selected library, however I also want the view to "Show all items without folders", which is an option in the Folders section of the View.

 

I've done some digging and found that the ViewScope field relates to this option, and I need to have it set to "Recursive", however none of the syntax that I try seems to work. The view creates but the "Show all items without folders" option isn't selected.

 

I'm quite new to PowerShell so I've tried to piece together bits and pieces I've found from different places, but not sure if I'm doing it right. The PowerShell I'm using is below:

Add-PnPView -List Current -Title "Test" -Fields "Type", "Name", "Modified", "Modified By" -Query "<View Scope='Recursive'></View>"

 

I'm basing this off another PowerShell script which IS working, as shown below:

Add-PnPView -List Current -Title "NewTest" -Fields "Type", "Name", "Modified", "Modified By" -Query "<Where><Eq><FieldRef Name = 'Surname' /><Value Type = 'Text'>Quentin</Value></Eq></Where>"

 

That PowerShell correctly creates the view and the filter is set to the chosen surname, in this case Quentin, so I know the syntax is correct, however I cannot get the ViewScope option to work. 

 

Any help would be greatly appreciated!

2 Replies
Best Response confirmed by StevieJDavis (New Contributor)
Solution

Hello @StevieJDavis,

I think you should create view first using Add-PnPView and then use Set-PnPView to set Values parameter for Recursive scope.

Set-PnpView -List $ListName -Identity $ViewName -Values @{Scope=[Microsoft.SharePoint.Client.ViewScope]::Recursive}

Reference.

Hope that helps.

@AndySvints that worked like a charm, thank you very much!