How to add column to view in all SharePoint sites

Brass Contributor

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.

 

column.PNG

6 Replies

Hello@elaheh 

 

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 

 

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
}
}

 

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

 

@elaheh 

 

Yeap but you can do for all sites ...

You just have to enumerate all sites and do an ForEach...

Hello

As I am very genereous

 

Function AddSensivityToDefaultView($URL)
{
    Try {
        Connect-PnPOnline -Url $URL -UseWebLogin -ErrorAction Stop
    }
    Catch
    {
        write-host "Error: $($_.Exception.Message) $($defaultViewInDocLib.Title )" -foregroundcolor Red
	}	
    Write-Host "I am in site $($URL)" 
    $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 Fields
		    $Fields= $defaultViewInDocLib.ViewFields
		    #Add Sensitivity Field in the list only if not already exists
            If ($Fields -notcontains "_DisplayName") {
		        $Fields += "Sensitivity"
                #Set View
                Try 
                {
		          Set-PnPView -List $docLib -Identity $defaultViewInDocLib.Title -Fields $Fields -ErrorAction Stop
                }
                Catch
                {
                    write-host "Error: $($_.Exception.Message) $($defaultViewInDocLib.Title )" -foregroundcolor Red
		        }	
		    }
            else {
               Write-Host "Already there in $($defaultViewInDocLib.Title)" 
            }
      
	    }
    }
}
$OrgName = "YOURORG"
Connect-PnPOnline -Url https://$OrgName-admin.sharepoint.com/ -UseWebLogin
$Sites = Get-PnPTenantSite 
Foreach ($Site in $Sites){
    
    if($Site.Url -ne "https://$OrgName-my.sharepoint.com/") {
        Try{
            AddSensivityToDefaultView($Site.Url) -ErrorAction Stop
        }
        Catch
        {
            write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
		}	
    }
}

 

Hi,

how to run this when not "site collection admin" on every site?
Thanks,
Daniel