Hi everyone,
So I am attempting to create a new view for a particular list in a large group of sub sites. I’ve got it to work, as written below, I am just missing how to add the grouping. I want to group the list items by business unit, then by subcategory. Can’t seem to get the syntax right.
#Get destination site and list
$web = Get-SPWeb http://mysharepoint.com/site
$list = $web.GetList(($web.ServerRelativeURL.TrimEnd(“/”) + “/ListName”))
$viewTitle = “Business Unit Grouped” #Title property
#Add the column names from the ViewField property to a string collection
$viewFields = New-Object System.Collections.Specialized.StringCollection
$viewFields.Add(“DocIcon”) > $null
$viewFields.Add(“LinkFilename”) > $null
$viewFields.Add(“Business Unit”) > $null
$viewFields.Add(“Subcategory”) > $null
$viewFields.Add(“Modified”) > $null
$viewFields.Add(“Editor”) > $null
#Query property
$viewQuery = “<OrderBy><FieldRef Name=’Modified’ Ascending=’FALSE’/></OrderBy>”
#RowLimit property
$viewRowLimit = 50
#Paged property
$viewPaged = $true
#DefaultView property
$viewDefaultView = $false
#Create the view in the destination list
$newview = $list.Views.Add($viewTitle, $viewFields, $viewQuery, $viewRowLimit, $viewPaged, $viewDefaultView)
Write-Host (“View ‘” + $newview.Title + “‘ created in list ‘” + $list.Title + “‘ on site ” + $web.Url)
$web.Dispose()
Sure, below is the script that worked. It was the field names at the end that was wrong. So while it would create the view the fields didn’t actually relate to columns. So it wouldn’t display any items and you could only delete the view with SP Designer. Once I got the proper field names using SharePoint Manager and entered those in it worked fine.
#Get destination site and list
$web = Get-SPWeb http://MySharePoint.com
$list = $web.GetList(($web.ServerRelativeURL.TrimEnd(“/”) + “/LibraryName”))
$viewTitle = “Business Units Grouped” #Title property
#Add the column names from the ViewField property to a string collection
$viewFields = New-Object System.Collections.Specialized.StringCollection
$viewFields.Add(“DocIcon”) > $null
$viewFields.Add(“FileLeafRef”) > $null
$viewFields.Add(“Business_x0020_Unit_x0020_ID”) > $null
$viewFields.Add(“Subcategory_x002d_RAS”) > $null
$viewFields.Add(“Year_x002d_RAS”) > $null
$viewFields.Add(“Modified”) > $null
$viewFields.Add(“Editor”) > $null
#Query property
$viewQuery = “<GroupBy Collapse=’TRUE’><FieldRef Name=’Business_x0020_Unit_x0020_ID’/><FieldRef Name=’Subcategory_x002d_RAS’/></GroupBy><OrderBy><FieldRef Name=’Modified’ Ascending=’FALSE’/></OrderBy>”
#RowLimit property
$viewRowLimit = 30
#Paged property
$viewPaged = $true
#DefaultView property
$viewDefaultView = $false
#Create the view in the destination list
$newview = $list.Views.Add($viewTitle, $viewFields, $viewQuery, $viewRowLimit, $viewPaged, $viewDefaultView)
Write-Host (“View ‘” + $newview.Title + “‘ created in list ‘” + $list.Title + “‘ on site ” + $web.Url)
$web.Dispose()