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?
2.5.1606.3
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
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..
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.
Seems to work in:
$view.ViewFields.Add("yourfieldname");
$view.Update();
$view.Context.ExecuteQuery();
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.
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.
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;}
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()
Yeah, what you鈥檝e 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.
We updated the Set-PnPView cmdlet with a -Fields option to set the fields
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
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
}
Most helpful comment
We updated the Set-PnPView cmdlet with a -Fields option to set the fields