Sunday, May 29, 2016

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

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
}



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...