Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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()
    } 
} 
share|improve this question
    
what user do you use in your PowerShell session? What output do you get? (does it enter the "updating" if's)? –  Anders Rask Sep 25 '12 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. –  user1697258 Sep 25 '12 at 15:08
add comment

1 Answer 1

up vote 0 down vote accepted

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

share|improve this answer
    
Thank you Anders. –  user1697258 Sep 27 '12 at 15:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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