1

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()
    } 
} 
2
  • what user do you use in your PowerShell session? What output do you get? (does it enter the "updating" if's)? Sep 25, 2012 at 13:55
  • Hi Anders, I'm using a site collection administrator account for the administrative powershell session. I get the output you'd expect, it enumerates all the subsites and enters the 'updating' ifs but when i look at the views on the site the queries have not changed. Sep 25, 2012 at 15:08

1 Answer 1

1

Try to assign the view to a variable inside the loop, as @james-love suggests here Why can't I update an SPView query?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.