Setting ListViewXml resets the view
Category
- [ ] Question
- [ ] Typo
- [x] Bug
- [ ] Additional article idea
Expected or Desired Behavior
We should be able to change the ListViewXml through the REST API. It did work before!
Observed Behavior
I've observed the same behavior using PnPJS and PnPPowershell.
When trying to change ListViewXml on a view, the view resets to default. Meaning in a document library you're left with DocIcon, Name, Modified and Editor. With a list only the Title column's left.
I'm seeing the on standaard and targeted release tenants.
Steps to Reproduce
It's easiest to reproduce using PnP Powershell like this:
$theList = "SomeList"; # The list to test with
$theView = "SomeView"; # The view to test with
# Get a view
$view = Get-PnPView -List $theList |? {$_.Title -eq $theView};
# Set the view (without changing it)
Set-PnPView -List $theList -Identity $view.Title -Values @{ListViewXml=$view.ListViewXml};
Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.
Can you please provide some context? SharePoint environment, classic / modern, etc...
The more context details you can provide, the easier it is to help assist on issues. Any code you can provide and/or screenshots of the issue also help. The easier you can make it to reproduce the issue, the easier and quicker it is for someone to help you. Things that always help posting with every issue include (1) SharePoint environment(s) (on-prem / SP 2016 / SP 2019 / SharePoint Online), (2) if SharePoint Online, is the tenant configured for standard / targeted release, (3) if SharePoint Framework, list the versions of installed tools (Node.js, NPM, Yeoman, Gulp, SPFx generator, etc... use npm list -g --depth=0), (4) if applicable, browser(s) tested, (5) if applicable, version of Office UI Fabric React & (6) anything else you can provide.
It's Sharepoint Online (as mentioned targeted and standard release). I've only looked at modern lists/libraries.
Use Fields to set the view instead of ListViewXml.
My test script:
Connect-PnPOnline -Url $siteURL -Credentials $PSCredentials
$theList = "TestList"; # The list to test with
$theView = "View2"; # The view to test with
#$targetView = "View1"; # The view to test with
# Get a view
$view = Get-PnPView -List $theList |? {$_.Title -eq $theView};
[xml]$Doc = New-Object System.Xml.XmlDocument
$Doc.LoadXml($view.ListViewXml);
$nodes = $Doc.SelectNodes("//View//ViewFields//FieldRef");
$Fields=New-Object System.Collections.Generic.List[System.String];
#[string[]]$Fields=@();
for($i=0;$i -lt $nodes.Count;$i++){
$Field = $nodes[$i].attributes['Name'].value
$Fields.Add($Field)
}
$FieldsList=$Fields -as [string[]]
$FieldsList.GetType()
Write-Host $FieldsList
# Set the view (without changing it)
Set-PnPView -List $theList -Identity $theView -Fields $FieldsList
@OS-Lee Thank you for the effort. It's a nice workaround, but...
This would double the API requests, because creating the view and adding the fields become separate requests. And I'm not actually using Powershell so this will require me to write/add an XML parser.
But I'm still going to look into a similar approach, because I expect it'll take some time before this bug is going to be squashed.
I know, it's a old issue, but maybe it'll help someone.
@RoelVB you need to set InnerXml of ListViewXml (without <View></View> tags).
Something like that:
[xml]$xmlDoc = New-Object System.Xml.XmlDocument
$xmlDoc.LoadXml($view.ListViewXml);
Set-PnPView -Web $web -List $listName -Identity $view.Title -Values @{ListViewXml = $xmlDoc.FirstChild.InnerXml };
If you don't want to add XML parser, you can just substract these tags from string.
This issue is being closed as part of an issue list cleanup project. Issues with no activity in the past 6 months that aren't tracked by engineering as bugs were closed as part of this inititive. If this is still an issue, please follow the steps outlined to re-open or submit a new issue.
Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues