Resolution:List View webpart sorting not working when added through powershell

Recently we faced a strange issue in one of our project. We added few list view webparts on different pages using powershell scripting as part of the deployment infrastructure.

It was the usual scripting used everywhere. The listview webparts were added properly to the page with the following script

 

 $web = Get-SPWeb   <site url>  
 $PubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web) 
 #Get the webpart manager
 $oWebPartManager = $web.GetLimitedWebPartManager(  <page url>  , [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);
 #Get the list
 $list = $web.Lists[  <List Name>  ];
 #Instantiate listview webpart and added to the page
 $listVWP = New-Object Microsoft.SharePoint.WebPartPages.XsltListViewWebPart;
$listVWP.Title =  <Listview webpart Title.  ;
$listVWP.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::None; 
$listVWP.Visible = $true;
$listVWP.EnableViewState = $true;
 $listVWP.ListName =   <List Name>;  
 $listVWP.ViewGuid =   <View Guid>  
$listVWP.WebId = $list.ParentWeb.ID;
$listVWP.ListId = [System.Guid]$list.ID;

 $oWebPartManager.AddWebPart($listVWP, "Main", 0);

 

Once added to the page we realized that the sorting and filtering option from the Out of the box sharepoint feature for these webparts not functioning. We tried few things during investigation to resolve the issue. We tried to add the same listview from UI and then introspect both the webparts from sharepoint designer.

But we couldn't figure out any fruitful solution. Then just realized the Savechanges method invocation is missing from our code hence added the below piece of code

 $oWebPartManager.SaveChanges($listVWP);
 This time once the deployment done to our surprise all seems to be working and ends with a happy note.
 Still figuring out what extra this single line of code leads to the magic :)