0

I have many Document Libraries that have the word 'Knowledge' in them. For example New Jersey Knowledge Library, New York Knowledge Library, etc.

I want to pull back all of these libraries and add a view to them. what is the proper syntax in PnP Powershell to do this? If the view already exists I don't want to add it again.

$lists = Get-PnPList -Web $web | Where-Object {'Knowledge' -in $_.name}

        foreach($list in $lists)
        {


        Write-Host "The name of the list is: " $list.name
                $views = Get-PnpView -List $list
                if('Knowledge Library' -notin $views.title)
                {
                    Write-Host "Knowledge Library View was not found found in one of the titles" -ForegroundColor Red
                    Add-PnpView -Title "Knowledge Library" -List $list.title -Fields "Type", "Title", "Content Author", "Published Date", "Abstract" -ViewType None -SetAsDefault
                    Write-Host "Created View for this Knowledge Library" -ForegroundColor Cyan
                }
          }  }

1 Answer 1

1

There are more elegant and maybe performant ways to do this, but you just want to get this done and move on with your life. Fetch all the lists, check its title against what you're doing, and if true, do it.

$lists = Get-PnPList -Web $web 

        foreach($list in $lists)
        {
            if($list.Title.ToLower().Contains("knowledge"))
            {
                # add the view here
            }

         }
1
  • Thanks for that! It's not letting me check it as the right answer? Commented Aug 28, 2019 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.