Create SharePoint Document Library View Filter Using PowerShell

James Shidell 116 Reputation points
2021-11-23T14:27:55.183+00:00

Hello,

I'm not sure what i'm doing wrong, but i'm trying to dynamically create a new view in a SharePoint 2016 Document Library and applying a filter based off a variable value. But it doesn't seem to be working, it always places the variable instead of the value. Below is my logic.

CREATE VIEWS

$viewTitle = $L1Folder
$viewFields = New-Object System.Collections.Specialized.StringCollection
$viewFields.Add("DocIcon") > $null
$viewFields.Add("LinkFilename") > $null
$viewFields.Add("Modified") > $null
$viewFields.Add("Title & Description") > $null
$viewFields.Add("Disposition Instructions") > $null
$viewFields.Add("Document Type") > $null
$viewQuery = '<OrderBy><FieldRef Name="LinkFileName" /></OrderBy>
<Where><Contains><FieldRef Name="File_x0020_Series"/><Value Type="Choice">$L1Folder</Value></Contains></Where>'
$viewRowLimit = 50
$viewPaged = $true
$viewDefaultView = $false
$newView = $docLibList.Views.Add($viewTitle,$viewFields,$viewQuery,$viewRowLimit,$viewPaged,$viewDefaultView)

Any suggestions? Thank you for any assistance.

Thank You

v/r
James

SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,236 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,686 questions
0 comments No comments
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 31,681 Reputation points Microsoft Vendor
    2021-11-24T02:46:09.387+00:00

    Hi @James Shidell ,
    Please refer to the following code. $L1Folder in Query is a variable, so we need to splicing two string instead of put them together.

    $viewTitle = $L1Folder  
    $viewFields = New-Object System.Collections.Specialized.StringCollection  
    $viewFields.Add("DocIcon") > $null  
    $viewFields.Add("LinkFilename") > $null  
    $viewFields.Add("Modified") > $null  
    $viewFields.Add("Title & Description") > $null  
    $viewFields.Add("Disposition Instructions") > $null  
    $viewFields.Add("Document Type") > $null  
    $viewQuery = '<OrderBy><FieldRef Name="LinkFileName" /></OrderBy>  
    <Where><Contains><FieldRef Name="File_x0020_Series"/><Value Type="Choice">' + $L1Folder + '</Value></Contains></Where>'  
      
    $viewInfo = New-Object Microsoft.SharePoint.Client.ViewCreationInformation  
    $viewInfo.Query = $viewQuery  
    $viewInfo.RowLimit = 50  
    $viewInfo.Paged = $true  
    $viewInfo.SetAsDefaultView = $false  
    $viewInfo.ViewFields = $viewFields  
    $viewInfo.Title = $L1Folder  
      
    $newView = $docLibList.Views.Add($viewInfo)  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. James Shidell 116 Reputation points
    2021-11-24T09:05:26.36+00:00

    You are the man! Awesome Thank you. I knew it was something simple I was missing.

    v/r
    James

    0 comments No comments