I'm using powershell to try to change the query for every view called either 'View Title 1' or 'View Title 2' in every list called 'News' in every subsite.
The script doesn't throw any errors but the views are not being updated.
Here is my code:
param($url = "")
if ($url -eq "")
{
Write-Warning "Specify a URL: -url ""http://site"""
exit
}
Function ReadWeb($web)
{
foreach ($subweb in $web.Webs)
{
ReadWeb($subweb)
}
for ($j = 0; $j -lt ($web.Lists.Count); $j++)
{
ProcessList($web.Lists[$j])
}
}
Function ProcessList($list)
{
if ($list.title -eq "List Title")
{
Write-Host "Processing web:" $list.ParentWebUrl
for ($i = 0; $i -lt ($list.views.Count); $i++)
{
if ($list.views[$i].title -eq "View Title 1")
{
write-host "Updating view:" $list.views[$i].title
$newQuery = '<CAML Query 1 />'
$list.views[$i].Query = $newQuery
$list.views[$i].Update()
}
elseif ($list.views[$i].title -eq "View Title 2")
{
write-host "Updating view:" $list.views[$i].title
$newQuery = '<CAML Query 2 />'
$list.views[$i].Query = $newQuery
$list.views[$i].Update()
}
}
}
}
$siteCollection = Get-SPSite $url
$WebApp = $siteCollection.WebApplication
foreach ($Site in $WebApp.Sites)
{
foreach($spWeb in $Site.AllWebs)
{
ReadWeb($spWeb)
$spWeb.Dispose()
}
}
EDIT - working code:
Function ProcessList($list) {
if ($list.title -eq "List Title")
{
Write-Host "Processing web:" $list.ParentWebUrl
for ($i = 0; $i -lt ($list.views.Count); $i++)
{
$currentView = $list.views[$i]
if ($currentView.title -eq "View Title 1")
{
write-host "Updating view:" $currentView.title
$newQuery = '<CAML Query 1 />'
$currentView.Query = $newQuery
$currentView.Update()
}
elseif ($currentView.title -eq "View Title 2")
{
write-host "Updating view:" $currentView.title
$newQuery = '<CAML Query 2 />'
$currentView.Query = $newQuery
$currentView.Update()
}
}
$list.update()
}
}