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()
Unfortunatly no, here is what I tried to run after Paul’s last reply.
$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 ID”) > $null
$viewFields.Add(“Subcategory – RAS”) > $null
$viewFields.Add(“Modified”) > $null
$viewFields.Add(“Editor”) > $null
#Query property
$viewQuery = “<OrderBy><FieldRef Name=’Modified’ Ascending=’FALSE’/></OrderBy><GroupBy Collapse = “TRUE”><FieldRef Name = “Business Unit ID”/></GroupBy>”
#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()
You need to put the GroupBy inside the quotes with the OrderBy.
Unfortunatly it still doesn’t work. It errors out on the part where I attempt to group it. This is the script I am trying to run:
#Get destination site and list
$web = Get-SPWeb http://mysharepointsite.com
$list = $web.GetList(($web.ServerRelativeURL.TrimEnd(“/”) + “/Library”))
$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 ID”) > $null
$viewFields.Add(“Subcategory – RAS”) > $null
$viewFields.Add(“Modified”) > $null
$viewFields.Add(“Editor”) > $null
#Query property
$viewQuery = “<OrderBy><FieldRef Name=’Modified’ Ascending=’FALSE’/></OrderBy>”
$viewQuery = “<GroupBy Collapse = “TRUE”><FieldRef Name = “Business Unit ID”/></GroupBy>”
#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()