Tuesday, August 25, 2015

Adding column in existing XLSTListViewwebpart in Powershell

SharePoint provides an option to show particular list view in another pages. We can able to show this using xsltlistviewwebpart. Most us met a situation to modify the listview after placed this in page. Here, i am going to provide a powershell script for this.

$web = Read-Host "Enter site URL : "

$spweb= Get-SPWeb $web


$list = $spweb.Lists["Your List Name"]
$field = $list.Fields["Column Name"]

$displayPage = $spweb.GetFile("SitePages/Home.aspx");
$wpManager = $spweb.GetLimitedWebPartManager($displayPage,[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

foreach($webpart in $wpManager.WebParts)

{
 if($webpart.Title -eq "Your webpart title")
    {
        Write-Host "Updating view" -ForegroundColor Magenta
        $lvwebpart = [Microsoft.SharePoint.WebPartPages.XsltListViewWebPart]$webpart
        $lvwebpart.View.ViewFields.Add($field)

        $lvwebpart.View.Update();
        Write-Host "view has been updated" -ForegroundColor Green
    }
}

No comments:

Post a Comment