Forum Discussion

elaheh's avatar
elaheh
Copper Contributor
May 19, 2020

How to add column to view in all SharePoint sites

Hi,

Is there any way or PowerShell script to add column synchronicity to view in SharePoint on all existing SharePoint sites? I have implemented AIP, and I would like to add ‘Sensitivity’ column in all SharePoint sites.

 

6 Replies

  • Helloelaheh 

     

    I am trying to do exactly the same thing (for me should be part of sensitivity label activation as an option by default but that is another discussion)...I have spent a lot of time on it already

     

    This script somehow works ...but when going back to UI Sensitivity column is not showing

     

     

    #Load SharePoint CSOM 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"
    
    Function Get-SPODocumentLibrary($SiteURL)
    {
        Try {
             
    			Write-host -f Yellow "Processing Site: $SiteURL"
    			#Get all document libraries - Exclude Hidden Libraries
    			$DocumentLibraries = Get-PnPList | Where-Object {$_.BaseTemplate -eq 101 -and $_.Hidden -eq $false} #Or $_.BaseType -eq "DocumentLibrary"
    	 
    			
    			#Loop through each document library and Get the Title
    			Foreach ($DL in $DocumentLibraries)
    			{
    				Write-host $DL.Title
    				#Select the default view
    				$Views = Get-PnPView -List $DL | Where-Object {$_.DefaultView -eq $true}
    				Foreach ($View in $Views)
    					{
    						Write-host -f Cyan $view.Title $DL.Title $SiteURL
    						$View.ViewFields.Add("_DisplayName")# Tried also with Sensitivity
    						$View.Update()
    						write-host "Field added to View!" -f Green
    						
    						
    					}
    				
    			}
            }
        Catch {
            write-host -f Red "Error Getting Document Libraries!" $_.Exception.Message
        }
    }
    
    
    #Config Parameters
    $AdminCenterURL = "https://orgname-admin.sharepoint.com"
     
    #Allow to use any account including one with MFA
    Connect-PnPOnline -Url $AdminCenterURL -UseWebLogin
    
    $sites = Get-SPOSite -Limit All
    
    #Go through each sites
     Foreach ($site in $sites)
            {
               write-host -f Green $site.url
               Get-SPODocumentLibrary $site.url
            }
    
     

     

    • Christophe Humbert's avatar
      Christophe Humbert
      Steel Contributor

      Christophe Humbert 

       

      With a bit of help from Veronique Lengelle 

      #Connect to site
      Connect-PnPOnline -Url https://TENANTNAME.sharepoint.com/sites/SITENAME -UseWebLogin
      
      $docLibraries = Get-PnPList | Where-Object {$_.BaseTemplate -eq 101 -and $_.Hidden -eq $false}
      
      foreach($docLib in $docLibraries){
      $defaultViewInDocLib = Get-PnPView -List $docLib | Where-Object { $_.DefaultView -eq $true}
      
      if ($defaultViewInDocLib){
      #Should Get existing Fieds
      $Fields= $defaultViewInDocLib.ViewFields
      #Add Sensitivity Field in the list
      $Fields += "Sensitivity"
      #Set View
      Set-PnPView -List $docLib -Identity $defaultViewInDocLib.Title -Fields $Fields
      }
      }

       

      • elaheh's avatar
        elaheh
        Copper Contributor

        Thanks a lot Christophe Humbert 

         

        btw, it seems it apply for only one site, while i need to do for all sites. 

        https://TENANTNAME.sharepoint.com/sites/SITENAME

         

Resources