0

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

}

}

# }

}

(Visited 23 times, 1 visits today)
Add a Comment