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()
I suspect the issue is either:
1) SharePoint GUI requires static/internal names for fields
OR
2) If your field has spaces in the name, you may need to use the special characters to replace the spaces.
If you create the view you want with the SharePoint web app and inspect the created view with SharePoint Manager, it may show your the expected syntax.
By the way… What version of SharePoint are you running?
I have not used that before but I’m looking into it right now. Thanks for the tip!
Ok, we’re definitely on the right track now. It did indeed create the view this time when using the format Richard had above. What’s really strange is after it creates the view and I browse the site, if you click modify this view, you get the following. Any other view you can modify without issue. Oh good times… : ) Thanks guys for all your help!
Correlation ID: 5b2a019d-2ad0-d01e-40e0-2cd987c893c2
Date and Time: 4/28/2015 9:40:32 AM
Ok… First, get rid of the double quotes and replace with singles. The outer double quotes contains all the inner singles…
$viewQuery = “<OrderBy><FieldRef Name=’Modified’ Ascending=’FALSE’/></OrderBy><GroupBy Collapse = ‘TRUE’><FieldRef Name = ‘Business Unit ID’/></GroupBy>”
I have done this in C# in code before. If you google “CAML C# groupby”, you should get some examples, although focused on C#, but it is generally the same idea.
If your OrderBy works, you are on the right track.
If I was grouping by ‘Title’ here is how it would look. You would need to add this to your $viewQuery line.
<GroupBy Collapse=’TRUE’>FieldRef Name=’Title’ /></GroupBy>
The result should be….
$viewQuery = “<GroupBy Collapse=’TRUE’>FieldRef Name=’Title’/></GroupBy><OrderBy><FieldRef Name=’Modified’ Ascending=’FALSE’/></OrderBy>”
I use Title as it is a simple example. Try using Title and let me know how it goes… If it works with Title, you know that you have the group by syntax working. Next, jump back to Business Unit ID.
By the way, have you used SharePoint Manager? You can browse the actual CAML queries created by the SharePoint GUI for views. Great tool.