kwizcom banner advertisement

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

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 modify view
  • In Modify View page, Select/Unselect the check boxes next to columns based on your requirement
    sharepoint online change list view
  • Click on Save to commit your changes.

SharePoint Online: PowerShell Script to Add a Field to an Existing List 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
SharePoint Online: Add-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 exists in the view!" -foregroundcolor Red 
    }

}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
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 5:17 PM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.