SOLVED

Add column to default view

Brass Contributor

Hi all,

 

is it possible to add additional column (ex language) to the default view on excisting and new document library on all sites?

I know how to do this on one document library, but dont know how i can set it so it will be default on all sites.

 

thanks!

5 Replies
Yes, you need to write some code / powershell to do that
best response confirmed by Off2w0rk (Brass Contributor)
Solution

HTH. Limitatons of this script are 

- Written using PnP PoserShell module 

- Can be used for single site

- Add additional column in default view only for existing sites. 

 

function ModifyView(){ Param(
  [Parameter(Mandatory=$True)]
  [Microsoft.SharePoint.Client.Web]$Web,

  [Parameter(Mandatory=$True)]
  [String]$ViewName, 

  [Parameter(Mandatory=$True)]
  [String]$Columnname

) 
 
$DocumentLibrarys=Get-SPOList |?{$_.Basetemplate -eq "101" -and $_.Hidden -eq $false -and $_.IsCatalog -eq $false }
foreach($DocumentLibrary in $DocumentLibrarys){
$view=$DocumentLibrary.Views
$context.Load($view)
$context.ExecuteQuery()
$view2 = $view | ? { $_.Title -eq $ViewName }
$view2.ViewFields.Add($Columnname);
$view2.Update();
$web.Context.ExecuteQuery()
}
Write-Host "All Doucument Library View Updated"
}

$o365=Get-Credential
$ViewName="All Documents"
$Columnname="Version"
Connect-SPOnline -Url https://<TenantName>.sharepoint.com/sites/teamsite -Credentials $o365
$context=get-spocontext
$web=$context.Web
$context.Load($web)
$context.ExecuteQuery()
ModifyView -web $web -ViewName $ViewName -Columnname $Columnname 

 

 

Other thing you could think about is creating a contenttype for that than you don't need code but need to walk trough libraries to set the contenttype 1 time.

I was going to suggest the same about content type.  This is a good reason to use content types, as they do make life much easier. 

Would you please share how to do this on one document library?

1 best response

Accepted Solutions
best response confirmed by Off2w0rk (Brass Contributor)
Solution

HTH. Limitatons of this script are 

- Written using PnP PoserShell module 

- Can be used for single site

- Add additional column in default view only for existing sites. 

 

function ModifyView(){ Param(
  [Parameter(Mandatory=$True)]
  [Microsoft.SharePoint.Client.Web]$Web,

  [Parameter(Mandatory=$True)]
  [String]$ViewName, 

  [Parameter(Mandatory=$True)]
  [String]$Columnname

) 
 
$DocumentLibrarys=Get-SPOList |?{$_.Basetemplate -eq "101" -and $_.Hidden -eq $false -and $_.IsCatalog -eq $false }
foreach($DocumentLibrary in $DocumentLibrarys){
$view=$DocumentLibrary.Views
$context.Load($view)
$context.ExecuteQuery()
$view2 = $view | ? { $_.Title -eq $ViewName }
$view2.ViewFields.Add($Columnname);
$view2.Update();
$web.Context.ExecuteQuery()
}
Write-Host "All Doucument Library View Updated"
}

$o365=Get-Credential
$ViewName="All Documents"
$Columnname="Version"
Connect-SPOnline -Url https://<TenantName>.sharepoint.com/sites/teamsite -Credentials $o365
$context=get-spocontext
$web=$context.Web
$context.Load($web)
$context.ExecuteQuery()
ModifyView -web $web -ViewName $ViewName -Columnname $Columnname 

 

 

View solution in original post