Ok, so here is what I am trying to accomplish. I have a client sites site collection. Under the root site is about 2,000 client sites. In each one is a document library, we will call it RAA, and it has three different views in it. We’d like to add a fourth view to each site that groups the documents first by one column and then by another. Since updating each site manually would take the rest of my life… PowerShell it is. I was able to create a script that added a column to each view, but how to I add a whole new view and group it?
This one I used to test against 3 sites, and it works to add a field to a view.
#Add-PSSnapin “Microsoft.SharePoint.PowerShell”
$url = “http://oursharepoint.com/client”
$site = Get-SPWeb ($url)
foreach ($web in $site.Site.AllWebs)
{
if ($web.Url.StartsWith($url) -and $web.Name.Contains(“0”))
{
Write-Host ($web.Name + “|” + $web.Url)
$list = $web.GetList(($web.ServerRelativeURL.TrimEnd(“/”) + “/RAS”))
Write-Host ($list.URL)
$view = $list.Views[“All Documents”]
Write-output (“List “ + $list.Title + “‘ on site “ + $webUrl)
Write-Host
# if ($web.Name.Contains(“00044”) -or $web.Name.Contains(“00047”) -or $web.Name.Contains(“00048”))
# {
try
{
Write-output (“Deleting fields on “ + $list.Title + “‘ on site “ + $webUrl)
$view.ViewFields.delete(“Year_x002d_RAS”)
$view.ViewFields.delete(“Modified”)
$view.ViewFields.delete(“Editor”)
$view.ViewFields.delete(“ArchiveStatus”)
$view.Update()
Write-output (“Adding the fields back on “ + $list.Title + “‘ on site “ + $webUrl)
# Add the fields back in the correct order
$view.ViewFields.add(“Business Unit ID”)
$view.ViewFields.add(“Year_x002d_RAS”)
$view.ViewFields.add(“Modified”)
$view.ViewFields.add(“Editor”)
$view.ViewFields.add(“ArchiveStatus”)
$view.Update()
}
catch
{
Write-Output (“There was a problem trying to update the view in the site “ + $webUrl + “: “ + $_)
write-output (” Error: ” + $_.Exception.ToString())
EXIT
}
}
# }
}
Hello Rick,
creating a view is not that hard. Please find an example of a function below.
function Create-View
{
param (
[Microsoft.SharePoint.SPWeb]$Web = $(throw “Required parameter -Web missing”),
[string]$LibraryName = $(throw “Required parameter -Library missing”),
[string]$ViewTitle = $(throw “Required parameter -ViewTitle missing”),
[string]$GroupField = $(throw “Required parameter -GroupField missing”),
[string]$OrderField = $(throw “Required parameter -OrderField missing”),
[System.Collections.Specialized.StringCollection]$ViewFields = $(throw “Required parameter -ViewFields missing”)
)
$library = $web.Lists[$LibraryName]
if ($library -ne $null)
{
if ($GroupField -eq “”)
{
if ($OrderField -eq “”)
{
$query = “<Where><Gt><FieldRef Name=””ID””/><Value Type=””Counter””>0</Value></Gt></Where>”
}
else
{
$query = “<Where><Gt><FieldRef Name=””ID””/><Value Type=””Counter””>0</Value></Gt></Where><OrderBy><FieldRef Name=””$OrderField”” Ascending=””FALSE”” /></OrderBy>”
}
}
else
{
if ($OrderField -eq “”)
{
$query = “<Where><Gt><FieldRef Name=’ID’/><Value Type=’Counter’>0</Value></Gt></Where><GroupBy Collapse=””FALSE”” GroupLimit=””30″”><FieldRef Name=””$GroupField”” /></GroupBy>”
}
else
{
$query = “<Where><Gt><FieldRef Name=’ID’/><Value Type=’Counter’>0</Value></Gt></Where><GroupBy Collapse=””FALSE”” GroupLimit=””30″”><FieldRef Name=””$GroupField”” /></GroupBy><OrderBy><FieldRef Name=””Modified”” Ascending=””FALSE”” /></OrderBy>”
}
}
$library.Views.Add($ViewTitle,$viewFields, $query, 100, $true, $true, “HTML”, $false) >> $null
}
}
To use this function :
# Create Views for the document libraries
[System.Collections.Specialized.StringCollection]$viewFields = New-Object System.Collections.Specialized.StringCollection
$viewFields.Add(“DocIcon”) >> $null
$viewFields.Add(“LinkFilename”) >> $null
$viewFields.Add(“Title”) >> $null
$viewFields.Add(“Modified”) >> $null
Create-View -Web $web -LibraryName “Documents” -ViewTitle “My view 1” -GroupField “EmploymentRegulationsCategory” -OrderField “Modified” -ViewFields $viewFields
Create-View -Web $web -LibraryName “Contracts” -ViewTitle “Summary” -GroupField “ContractCategory” -OrderField “Modified” -ViewFields $viewFields
Should get you started…
Bart