SharePoint Online: Add-Remove Fields to List View using PowerShell

Requirement: SharePoint Online PowerShell to Update a List View

How to Add or Remove Columns in SharePoint Online List View?
To add or remove columns in SharePoint online list view, follow these steps:
  • Navigate to your SharePoint Online List >> Click on Modify View button from List Tab of the ribbon
    sharepoint online powershell add field to view
  • In Modify View page, Select/Unselect the check boxes next to columns based on your requirement
    sharepoint online powershell add column to view
  • Click on Save to commit your changes.
Now, lets add a column to SharePoint Online view using PowerShell.

SharePoint Online: PowerShell to Add a Field to View
Here is the PowerShell to add a field to an existing List view in SharePoint Online.
#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
##Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/Sales/"
$ListName="Documents"
$FieldToAdd="ID"
$UserName="Salaudeen@crescent.com"
$Password ="Password goes here"

Try {
    
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))

    #Get Web information and subsites
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $credentials
    
    #get the list
    $List = $Context.web.lists.GetByTitle($Listname)
    
    #Check if View has the field already
    $ViewFields = $List.DefaultView.ViewFields
    # Get a specific list view by Name
    #$view=$List.Views.getByTitle($ViewName)
    $Context.load($ViewFields)  
    $Context.ExecuteQuery()  

    if( ($ViewFields -Contains $FieldToAdd) -eq $false) {
        #Add the Field to View
        $List.DefaultView.ViewFields.Add($FieldToAdd)
        $List.DefaultView.Update()
        $Context.ExecuteQuery()

        Write-host "List View updated!" -ForegroundColor Green 
    }
    else { 
    write-host "Field exists in the view already!" -foregroundcolor Red 
    }

}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
The above script adds "ID" field to the default view of the given library

Add Column to List View using PnP PowerShell:
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$ListName= "Projects"
$ViewName= "Active Projects"
$ColumnName = "Category" #Internal Name

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PNP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred

    #Get the Context
    $Context = Get-PnPContext

    #Get the List View from the list
    $ListView  =  Get-PnPView -List $ListName -Identity $ViewName -ErrorAction Stop

    #Check if view doesn't have the column already
    If($ListView.ViewFields -notcontains $ColumnName)
    {
        #Add Column to View
        $ListView.ViewFields.Add($ColumnName)
        $ListView.Update()
        $Context.ExecuteQuery()
        Write-host -f Green "Column '$ColumnName' Added to View '$ViewName'!"
    }
    else
    {
        Write-host -f Yellow "Column '$ColumnName' Already Exists in View '$ViewName'!"
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
} 
SharePoint Online: Add or Remove Fields to List View using PowerShell

Remove Field from the List View using PowerShell in SharePoint Online:
Similarly, to remove a field from the view, the PowerShell script goes like:
#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/Sales/"
$ListName="Documents"
$FieldToAdd="ID"
$UserName="Salaudeen@crescent.com"
$Password ="Password goes here"

Try {    
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))

    #Setup the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $credentials
    
    #get the list
    $List = $Context.web.lists.GetByTitle($Listname)
    
    #Check if the View has the field in it
    $ViewFields = $List.DefaultView.ViewFields
    $Context.load($ViewFields)  
    $Context.ExecuteQuery()  

    if( ($ViewFields -Contains $FieldToAdd) -eq $true) {
        #Add the Field to View
        $List.DefaultView.ViewFields.Remove($FieldToAdd)
        $List.DefaultView.Update()
        $Context.ExecuteQuery()

        Write-host "Field Has been removed from the List View!" -ForegroundColor Green 
    }
    else { 
    write-host "Field doesn't exist in the view!" -foregroundcolor Red 
    }

}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Remove Column from SharePoint Online View using PnP PowerShell:
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$ListName= "Projects"
$ViewName= "Active Projects"
$ColumnName = "Category" #Internal Name

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PNP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred

    #Get the Context
    $Context = Get-PnPContext

    #Get the List View from the list
    $ListView  =  Get-PnPView -List $ListName -Identity $ViewName -ErrorAction Stop

    #Check if view doesn't have the column already
    If($ListView.ViewFields -contains $ColumnName)
    {
        #Add Column to View
        $ListView.ViewFields.Remove($ColumnName)
        $ListView.Update()
        $Context.ExecuteQuery()
        Write-host -f Green "Column '$ColumnName' Removed from View '$ViewName'!"
    }
    else
    {
        Write-host -f Yellow "Column '$ColumnName' doesn't exist in View '$ViewName'!"
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
If you need SharePoint Online PowerShell to Update List View, such as View Filter, refer: SharePoint Online: PowerShell to Update View
SharePoint Online: Add-Remove Fields to List View using PowerShell SharePoint Online: Add-Remove Fields to List View using PowerShell Reviewed by Salaudeen Rajack on May 29, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.