PnP-PowerShell icon indicating copy to clipboard operation
PnP-PowerShell copied to clipboard

How to add Fields to a View ?

Open Land3r opened this issue 9 years ago • 12 comments

Reporting an Issue or Missing Feature

  • [ ] Bug
  • [X] Question

I am using the powershell module to programatically create Site Fields, add them to Site Content Types and then create Lists based on thoses Content Types. However I didn't find a way to add the fields used in the Content Type in the Default View of the List. Is there a recommended way or best practice to do such a thing or do I have to use "regular" Sharepoint CSOM ?

By "regular" Sharepoint CSOM, i mean something like this:

$DefaultView = $List.DefaultView
$DefaultView.ViewFields.Add("City")
$DefaultView.ViewFields.Add("Company")
$DefaultView.Update()
$Context.ExecuteQuery()

And by the way, how do you retrieve the context based on a Connect-SPOnline call?

Which version of the PnP-PowerShell Cmdlets are you using?

  • [ ] PnP PowerShell for SharePoint 2013
  • [ ] PnP PowerShell for SharePoint 2016
  • [X] PnP PowerShell for SharePoint Online

What is the version of the Cmdlet module you are running?

2.5.1606.3

How did you install the PnP-PowerShell Cmdlets?

  • [ ] MSI Installed downloaded from GitHub
  • [X] Installed through the PowerShell Gallery with Install-Module
  • [ ] Other means

Land3r avatar Mar 29 '17 22:03 Land3r

I could not figure out how to do this either. Ended up creating a new default view Add-PnPView -list $l -Title 'test view' -SetAsDefault -Fields @('ApplicationType','Title')

Would be nice if there were a PNP way to add fields to existing view

russgove avatar Apr 18 '17 20:04 russgove

Ah, I think I figured it out . view.ViewFields is a Microsoft.SharePoint.Client.ViewFieldCollection That collection has a n Add member. This does not show up when I do $v.viewFields |get-member for some reason. But the method does work I can call $v.ViewFields.Add('Title') and it adds the title to the view..

russgove avatar Apr 18 '17 20:04 russgove

Well, i thought I had it figure out. I can call $v.ViewFields.Add('Title') and it adds the title to the view.... but only in my local copy of ViewFields. It does not get saved back to sharepoint. Looks like this is a CSOM limitation ViewFields is read-only: public ViewFieldCollection ViewFields { get; }

I see the ListViewXml is not read-only, so maybe fields could be added that way.

russgove avatar Apr 19 '17 00:04 russgove

Seems to work in: $view.ViewFields.Add("yourfieldname"); $view.Update(); $view.Context.ExecuteQuery();

koenti avatar Apr 26 '17 21:04 koenti

I was looking at adding columns / fields to the default view of document library, and I still didn't find a way to do it with the PnP cmdlets. However indeed using CSOM "methods" works fine.

Not sure why they didn't make it possible in the Set-PnPView cmdlet. Maybe the recommended way to customize view is to create a new one and set it as default.

mrik23 avatar Jan 29 '18 07:01 mrik23

I'm using Add-PnPView to create a new view and remove the old one. But in the modern view this new view gives an error. The view works in the classic view. Creating the same view in the UI gives no problem.

Frederik80 avatar Oct 18 '18 11:10 Frederik80

I hit this problem as well. It's odd that it works with regular CSOM but not Set-PnPView.

However when I performed a Get-Member it appears that the ViewFieldCollection doesn't have setter which is likely the cause for failure, maybe this property is unique and needs to be handled differently than others?

TypeName : Microsoft.SharePoint.Client.View Name : ViewFields MemberType : Property Definition : Microsoft.SharePoint.Client.ViewFieldCollection ViewFields {get;}

JaredRic avatar Oct 22 '18 22:10 JaredRic

I figured it out with PnP $AllDocView = Get-PnPView -List $name -Identity "All Documents" $AllDocView.ViewFields.Add("Agreement Type") $AllDocView.ViewFields.Add("Document Type") $AllDocView.ViewFields.Add("Document Owner") $AllDocView.Update() $context.ExecuteQuery()

hikmatune avatar Mar 21 '19 20:03 hikmatune

Yeah, what you’ve got there is a hybrid of PnP and regular CSOM to add it. I think what we were looking to work is the Set-PnPView command to handle that in the background as that is what the module does, limit the need for regular CSOM.

JaredRic avatar Mar 21 '19 21:03 JaredRic

We updated the Set-PnPView cmdlet with a -Fields option to set the fields

erwinvanhunen avatar Mar 21 '19 21:03 erwinvanhunen

I noticed that when adding fields using the command it can add the same field multiple times which the UI doesn't allow. Does it make sense to have a check prior to adding the field to make sure we're not adding the same field more than once to a view? Example would be running a script like this twice:

$list = "TestPnpViews" $view = "All Items" $fieldsToAdd = "Created", "Author"

$fields = (Get-PnpView -List $list -Identity $view).ViewFields $fields += $fieldsToAdd Set-PnPView -List $list -Identity $view -Fields $fields

The result looks something like this:

PS C:> $fields LinkTitle Created Author Created Author

JaredRic avatar Mar 21 '19 23:03 JaredRic

Just tweak your code with an If condition to avoid double adding if(!$fields.contains($FieldName)) { $fields += $FieldName Set-PnPView -List $ListTitle -Identity $ViewName -Fields $fields }

DawsyB avatar Jul 09 '19 03:07 DawsyB