Change 'Selected View' of Document Library Web Part using Powershell (PnP) on Prem.

James Shidell 116 Reputation points
2021-12-03T10:32:37.107+00:00

Hello,

I'm running into something strange that I just can't seem to figure out.
I'm dynamically adding document library web parts to a Publishing Web Part page based off of views using PowerShell PnP. This seems to be working fine somewhat.

I'm creating the views dynamically and setting the 'Show All Items Without Folders' for each view.

$targetView = Get-PnPView -List <List Name> -Identity <View Title>
$targetView.Scope = "Recursive"
$targetView.Update

After creating the view, if I go to the view page itself (https://portal/SiteA/DocLib/Forms/View1.aspx) and I modify the view I see that the 'Show All Items Without Folders' is selected.

However when I add those Document Library Web Part Views to my Publishing Web Part Page using Powershell PnP the Web Parts are added successfully, but when I 'Edit Web Part' and then 'Edit the current view' I notice that the 'Show All Items Without Folders' is no longer selected. All other settings for the views are all still set correctly (selected fields/column, filtering, etc).

What is strange is if I 'Edit Web Part' and click the dropdown under 'Selected View' I see the selected view (View1) with <Current View> indented under it.

View1
  <Current View>

To me that looks like the correct view is selected (View1). However if I actually select the view name and click apply, all is good, the 'Show All Items Without Folders' is now set.

Right now I have to go to all the web parts, and select the view name manually and apply, even though It looks like the view is already selected for that web part.

Any ideals on what I can do to update the 'Selected View' of the Web Parts using Powershell PnP?

Am I missing anything simple?

Thank you for any assistance

v/r
JShidell

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,561 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,557 questions
SharePoint Server Management
SharePoint Server Management
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Management: The act or process of organizing, handling, directing or controlling something.
2,765 questions
0 comments No comments
{count} votes

Accepted answer
  1. James Shidell 116 Reputation points
    2021-12-08T11:09:24.603+00:00

    I was finally able to figure out my solution. I couldn't get the 'selected view' to change, but that is ok, the view was always there, the issue I was having was with the Scope, even though the Scope was set correctly in the Views its was not set correctly in the Web Part. I needed to update the Scope in the Web Part to be "Recursive". Below is how I accomplished this for anyone interested.

    $web = Get-SPWeb "https://portal/siteA"
    $pubWebPageUrl = $web.ServerRelativeUrl + "/" + "Pages" + "/" + "Tabs.aspx"
    $pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
    $AllUnsafeUpdatesStatusWeb = $web.AllUnsafeUpdates
    $web.AllowUnsafeUpates = $true
    $culture = New-Object System.Globalization.CultureInfo("en-US")
    $wpManager = $web.GetLimitedWebPartManager($pubWebPageUrl,[System.Web.Ui.WebControls.WebParts.PersonalizationScope]::Shared)
    $webParts = $wpManager.WebParts | ? {$_.Title -eq  "Web Part Title"}
    $wpOnPage = $webParts.GetType()
    if ($wpOnPage.Name -eq "XsltListViewWebPart")
    {
    $wpID = $webParts.ViewGuid.ToString()
    $page = $pubPage.GetPublishingPage(pubWebPageUrl)
    $page.CheckOut()
    $list = $web.Lists[DOCLIB]
    $view = $list.Views[VIEW TITLE]
    $wpView = $list.Views | ? {$_.ID -eq $wpID}
    $wpView.GetType().InvokeMember("EnsureFullBlownXmlDocument",[Reflection.BindingFlags] "NonPublic, Instance, InvokeMethod", $null, $wpView, $null, $culture)
    $wpView.Scope = "Recursive"
    $wpView.Update()
    $page.Update()
    $item = $page.ListItem
    $item.File.Checkin("")
    $istem.File.Publish(" ")
    }
    $web.Dispose()
    

    Thanks for all the assistance you guys provided with helping.

    v/r
    JShidell


3 additional answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 29,041 Reputation points Microsoft Vendor
    2021-12-06T06:46:09.003+00:00

    Hi @James Shidell ,
    That is not updated when you change the view on the library itself. You have to update the view on the web part.

    Current View is simply the current view of the library web part. It is the default name given to the view that is used when the list web part is added to the page. Editing / Modifying the view on the web part will not affect the views on the library itself.

    If you create a custom view on the library, you can select that as the basis for the web part view, but again changes to the view on the library itself will not be reflected on the web part or vice versa.

    So you can only change the web part view in Edit Web Part
    155130-image.png


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    1 person found this answer helpful.

  2. sadomovalex 3,621 Reputation points
    2021-12-03T16:14:42.46+00:00

    try also update web part itself (may be you should use Get-PnPWebPart for that) and may be https://portal/SiteA/DocLib/Forms/View1.aspx page as well. I remember there was similar issue with old one-prem ListViewWebParts - when you modified some list view you also needed to checkout page, save changes also in web part and then check it in. I.e. it was not enough to update list view itself.


  3. James Shidell 116 Reputation points
    2021-12-06T11:02:55.107+00:00

    Would anyone know how to update the XmlDefintion of the Web Part Properties using PnP? If so I might have found a way. I tried to do it like this, below but it didn't work.

    Set-PnPWebPartProperty -ServerRelativeUrl "/SC/Pages/Tabs.aspx" -Identity <GUID of Web Part> -Key "XmlDefnition" -Value '<View name={GUID} Scope="Recursive"</view>'
    

    Within the XML Definition I added Scope="Recursive", of course all the other XML elements stayed the same. I added the Scope="Recursive" after the Hidden Tag and DisplayName tag ie. Hidden="True" Scope="Recursive" DisplayName=""

    But the Set-PnPWebPartProperty doesn't seem to update the XmlDefinition. What would be the proper way to do this?

    Thank you

    v/r
    JShidell