SharePoint Online (Office PnP): Populate default fields in View dynamically

Scenarios: How to add all fields into view dynamically

Code Snippet

Function AddDefaultFieldsToView {
	Param (

	$view = Get-PnPView -Identity $ViewName -List $ListName
	if($view -eq $null) {
		Write-Host "View $ViewName does not exist in $ListName"

	$ct = Get-PnPContentType -InSiteHierarchy -Identity $ContentTypeName	
	$columns = Get-PnPProperty -ClientObject $ct -Property "Fields"

	Write-Host "Applying default columns to View: '$ViewName' from ContentType: '$ContentTypeName'"
    foreach ($column in $columns | Where-Object { !$_.Hidden } ) {        
		if($view.ViewFields -contains $column.InternalName){
			Write-Host ("'" + $column.Title + "' already exist in View")

        if ($column.Title -match "Modified By" -or
			$column.Title -match "Created By" -or
			$column.Title -eq "Name" -or
			$column.Title -eq "Content Type" -or
			$column.Title -eq "Title"
		) {
			#system fields				
		Write-Host ("Added field '" + $column.Title + "' to '" + $ViewName + "'")

	Write-Host "Applied default columns"

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s