SharePoint Online - Create list view with PowerShell and create filter

This script allows to add a new view to an existing List in a SharePoint Online site. 

The script defines a function that uses the Client Side Object Model (CSOM) for SharePoint Online to create a view in an existing List. Before adding the view to the list, some configurations are defined in the body of the function: Title, Row Limit, Type of View, etc.

Source: https://gallery.technet.microsoft.com/office/How-to-create-a-List-View-fb4782c9



############################################################################################################################################ 
#Script that allows to create a new view in a SharePoint Online List 
# Required Parameters: 
#  -> $sUserName: User Name to connect to the SharePoint Online Site Collection. 
#  -> $sPassword: Password for the user. 
#  -> $sSiteUrl: SharePoint Online Site Url. 
#  -> $sListName: Name of the list where the new view is going to be added. 
#  -> $sViewName: Name of the view to be added. 
############################################################################################################################################ 
 
$host.Runspace.ThreadOptions = "ReuseThread" 
 
#Definition of the function that allows to create a new view in a SharePoint Online list 
function Create-NewListViewSPO 
{ 
    param ($sSiteUrl,$sUserName,$sPassword,$sListName,$sViewName) 
    try 
    {     
        #Adding the Client OM Assemblies         
        Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.dll" 
        Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.Runtime.dll" 
 
        #SPO Client Object Model Context 
        $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl) 
        $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName$sPassword)   
        $spoCtx.Credentials = $spoCredentials       
 
        Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green 
        Write-Host "Adding the View $sViewName to the List $sListName !!" -ForegroundColor Green 
        Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green         
 
        #Getting the list to be updated with a new view         
        $spoList=$spoCtx.Web.Lists.GetByTitle($sListName) 
        $spoCtx.Load($spoList) 
 
        #Defining the new List View 
        $spoViewCreationInformation=New-Object Microsoft.SharePoint.Client.ViewCreationInformation 
        $spoViewCreationInformation.Title=$sViewName 
        $spoViewCreationInformation.ViewTypeKind= [Microsoft.SharePoint.Client.ViewType]::None         
        $spoViewCreationInformation.RowLimit=30 
        $spoViewCreationInformation.SetAsDefaultView=$true         
        $spoViewCreationInformation.ViewFields=@("Title","Created","Modified")         
 
        #Getting the collection of views of the List 
        $spoListViews=$spoList.Views 
        $spoCtx.Load($spoListViews)              
        $spoCtx.ExecuteQuery()                 
        $spListViewToAdd=$spoListViews.Add($spoViewCreationInformation) 
 
        #Adding the view to the List 
        $spoCtx.Load($spListViewToAdd)                 
        $spoCtx.ExecuteQuery() 
        Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green 
        Write-Host "View $sViewName added to the List $sListName !!" -ForegroundColor Green 
        Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green   
        $spoCtx.Dispose() 
    } 
    catch [System.Exception] 
    { 
        write-host -f red $_.Exception.ToString()    
    }     
} 
 
#Required Parameters 
$sSiteUrl = "https://<O365Domain>.sharepoint.com/<SPO_Site>"  
$sUserName = "<O365User>@<O365Domain>.onmicrosoft.com"  
$sListName"<SPO_List_Name>" 
$sViewName="<SPO_View_Name>" 
#$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString   
$sPassword=convertto-securestring "<SPO_Password>" -asplaintext -force 
 
Create-NewListViewSPO -sSiteUrl $sSiteUrl -sUserName $sUserName -sPassword $sPassword -sListName $sListName -sViewName $sViewName 
More Resources


Alternatevely, Here is a tweak I made to this script to:
  • Add in a filter on the view
  • Create a bunch of views based on the values in a text file (each new one on a separate line)
    • I put the file in D:\tools so change that to wherever you have the text file
############################################################################################################################################
#Script that allows to create a new view in a SharePoint Online List
# Required Parameters:
#  -> $sUserName: User Name to connect to the SharePoint Online Site Collection.
#  -> $sPassword: Password for the user.
#  -> $sSiteUrl: SharePoint Online Site Url.
#  -> $sListName: Name of the list where the new view is going to be added.
#  -> $sViewName: Name of the view to be added.
############################################################################################################################################

$host.Runspace.ThreadOptions = "ReuseThread"

#Definition of the function that allows to create a new view in a SharePoint Online list
function Create-NewListViewSPO
{
    param ($sSiteUrl,$sUserName,$sPassword,$sListName,$sViewName,$sViewQuery)
    try
    {    
        #Adding the Client OM Assemblies        
        #Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.dll"
        #Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.Runtime.dll"

        #SPO Client Object Model Context
        $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl)
        $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName, $sPassword)  
        $spoCtx.Credentials = $spoCredentials      

        Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green
        Write-Host "Adding the View $sViewName to the List $sListName !!" -ForegroundColor Green
        Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green        

        #Getting the list to be updated with a new view        
        $spoList=$spoCtx.Web.Lists.GetByTitle($sListName)
        $spoCtx.Load($spoList)

        #Defining the new List View
        $spoViewCreationInformation=New-Object Microsoft.SharePoint.Client.ViewCreationInformation
        $spoViewCreationInformation.Title=$sViewName
        $spoViewCreationInformation.ViewTypeKind= [Microsoft.SharePoint.Client.ViewType]::None        
        $spoViewCreationInformation.RowLimit=50
        $spoViewCreationInformation.SetAsDefaultView=$false        
        $spoViewCreationInformation.ViewFields=@("Last Name","First Name","Job Function","Phone","Email Address")
        $spoViewCreationInformation.Query=$sViewQuery

        #Getting the collection of views of the List
        $spoListViews=$spoList.Views
        $spoCtx.Load($spoListViews)             
        $spoCtx.ExecuteQuery()                
        $spListViewToAdd=$spoListViews.Add($spoViewCreationInformation)

        #Adding the view to the List
        $spoCtx.Load($spListViewToAdd)                
        $spoCtx.ExecuteQuery()
        Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green
        Write-Host "View $sViewName added to the List $sListName !!" -ForegroundColor Green
        Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green  
        $spoCtx.Dispose()
    }
    catch [System.Exception]
    {
        write-host -f red $_.Exception.ToString()   
    }    
}

#Required Parameters
$views = Get-Content "D:\Tools\viewlist.txt"
foreach($view in $views) {
try {
$sSiteUrl = "https://<O365Domain>.sharepoint.com/<SPO_Site>"  
$sUserName = "<O365User>@<O365Domain>.onmicrosoft.com" 
$sListName"<SPO_List_Name>" 
$sViewName=$view
$sViewQuery="<OrderBy><FieldRef Name='Last Name' /></OrderBy><Where><Eq><FieldRef Name='Supplier' /><Value Type='Text'>" + $view + "</Value></Eq></Where>"
#$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString  
$sPassword=convertto-securestring "<SPO_Password>" -asplaintext -force 

Create-NewListViewSPO -sSiteUrl $sSiteUrl -sUserName $sUserName -sPassword $sPassword -sListName $sListName -sViewName $sViewName -sViewQuery $sViewQuery
}
catch {
            "Invalid view or view may already exist: $view" | Out-File "D:\Tools\viewlist_invalid.log" -Append
        }
}


Share on Google Plus

About Tom DeMeulenaere

Highly accomplished information technology professional with extensive knowledge in System Center Configuration Manager, Windows Server, SharePoint, and Office 365.
    Blogger Comment

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.