Add-PnpView not applying filter

Jurgen Verhelst 326 Reputation points
2020-12-27T16:02:46.013+00:00

Hi

trying to get a filtered view in a modern teams site template to the default documents lib
before I could just create a template (get-pnpProvisioningTemplate) , then apply that to the new site, but now it complains that documents already exist. before it just added the new fields and views on top of it.

so now I removed the documents lib from the xml template and try to recreate the fields and views after the site creation and before applying the xml template

but, when I try to add the view, it is created but the filter does something funky:

used code:

# Add Columns to Documents Library  
Add-PnPField -List "Documents" -DisplayName "Show In Home" -InternalName "Show In Home" -Type boolean -AddToDefaultView  
  
# Add View to Documents Library  
Add-PnPView -Fields "DocIcon","LinkFilename","Modified","Editor","_UIVersionString","FileSizeDisplay" -List "Documents" -Title "Show In Home" -Query "<OrderBy><FieldRef Name='Prio' Ascending='false' /></OrderBy><Where><Eq><FieldRef Name = 'Show In Home' /><Value Type = 'Boolean'>1</Value></Eq></Where>" | out-Null  
 

(I added the out-Null because the runbook tried to run three times otherwise)

The view gets created but the filter where "Show in home" is equal to '1' is not working
the field is None instead of 'Show In Home' and also it does not understand '1', it needs to be Yes before I can save the view in SPO

I tried using 'Yes' in de CAML but that does not take

any ideas?

also if someone could show me how I could type the add-pnpview in several lines so its not that long would be nice too :)

here is the screenshot how it looks in SPO when the powershell ran:
51452-screenshot-2020-12-27-164402.png

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,406 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,320 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 94,196 Reputation points MVP
    2020-12-27T21:28:06.31+00:00

    I am not 100% if the -InternalName is working with a space in the value:

     # Add Columns to Documents Library
     Add-PnPField -List "Documents" -DisplayName "Show In Home" -InternalName "ShowInHome" -Type boolean -AddToDefaultView
    
     # Add View to Documents Library
     Add-PnPView -Fields "DocIcon","LinkFilename","Modified","Editor","_UIVersionString","FileSizeDisplay" -List "Documents" -Title "Show In Home" -Query "<OrderBy><FieldRef Name='Prio' Ascending='false' /></OrderBy><Where><Eq><FieldRef Name = 'ShowInHome' /><Value Type = 'Boolean'>1</Value></Eq></Where>" | out-Null
    

    Maybe this is working as expected?


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Jurgen Verhelst 326 Reputation points
    2020-12-27T21:47:20.473+00:00

    Thanks Andreas!

    I will never use spaces in internal names again
    I will never use spaces in internal names again
    I will never use spaces in internal names again
    I will never use spaces in internal names again

    :)


  2. Jurgen Verhelst 326 Reputation points
    2020-12-27T22:13:17.78+00:00

    coul it be I need to wait a while after the fields have been added?

    I remove the out-null and got this error

    format-default : The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

    • CategoryInfo : NotSpecified: (:) [format-default], CollectionNotInitializedException
    • FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException,Microsoft.PowerShell.Commands.FormatDefaultCommand

    i do see the items getting created though...

    other post indicate I need to load the collection but thats not pnp but sharepoint PS and not to familiar with it. It works but would like to get the error out of the way

    #Setup the Context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
    
    #Get All Fields from the List
    $List = $Ctx.Web.Lists.GetByTitle("Documents")
    
    #Load List Fields collection
    $Ctx.Load($List.Fields)
    $Ctx.ExecuteQuery()
    
    
    #Read more: https://www.sharepointdiary.com/2018/06/sharepoint-online-powershell-the-collection-has-not-been-initialized-error.html#ixzz6hrhfpIfC
    
    0 comments No comments

  3. Andreas Baumgarten 94,196 Reputation points MVP
    2020-12-27T22:39:30.323+00:00

    Have you tried to write the last command in a variable?

    It doesn't fix the issue ... but you shouldn't see the message ;-)

     # Add Columns to Documents Library
      Add-PnPField -List "Documents" -DisplayName "Show In Home" -InternalName "ShowInHome" -Type boolean -AddToDefaultView
    
      # Add View to Documents Library
      $magic = Add-PnPView -Fields "DocIcon","LinkFilename","Modified","Editor","_UIVersionString","FileSizeDisplay" -List "Documents" -Title "Show In Home" -Query "<OrderBy><FieldRef Name='Prio' Ascending='false' /></OrderBy><Where><Eq><FieldRef Name = 'ShowInHome' /><Value Type = 'Boolean'>1</Value></Eq></Where>"
    

    Not sure if this message is just a bug.


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  4. Jurgen Verhelst 326 Reputation points
    2020-12-27T22:48:24.587+00:00

    Strange,

    the magic variable works but no message, so tried the original code, no message. maybe I do need to wait a few seconds between adding the fields and adding the view?

    so both methods work and add the new views "show in home magic" and "show in home oldway", correctly

    Why did you suggest adding the command in a variable?

    And is there a pnp command to 'inititalize the collection'? or should it automaticaly inititialise anyway?

    0 comments No comments