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()
Can you share the final powershell for posterity’s sake?
I would be interested to see the functioning final result.
Glad to hear its working!
Well figured it out. while it did create the views those field names did not matchup. After using the tool you suggest, thanks for that!, I was able to add in the correct field names and the script works great. So a few thousand sites just got their new view. Thanks guys!
Have you tried reviewing the definition of a normal view created in the SharePoint web app? You may be able to do this through powershell also, but I’m not sure how to do that without researching it.
Hmm, this one is tough to figure out. I tried different field names, different kinds of fields to group by, any view I create via PS results in a faulty view. Not only does it error when you click modify this view it also does not show any items in the library.
I’m just digging through logs hoping to find something at this point.