更新列表视图 SharePoint PowerShell

updating List views SharePoint PowerShell

我希望能够更新两个不同列表中所有视图的“JSLink”属性。但是我不断收到以下错误:

“集合已修改;枚举操作可能无法执行。”

我有以下 PowerShell:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Temp Site
$siteColl = "mysitecollectionURL"

$site = Get-SPSite $siteColl
 
#JS file to add
$jSFile = "clienttemplates.js"

#Lists to update
$masterTasks = "Master Tasks"
$allTasks = "All Tasks"

        Write-Host "`nChecking site:" $site.RootWeb.URL
        
        #Get root web
        $web = $site.RootWeb
        
        #Iterating lists
        foreach($list in $web.Lists)
        {   
            if($list.Title -eq $masterTasks)
            {
                write-host "`n`tMaster Tasks list found"
                
                #Iterating views
                foreach($view in $list.Views)
                {
                        $view.JSLink = $jSFile
                        $view.Update()
                }
            }
            
            if($list.Title -eq $allTasks)
            {
                write-host "`n`tAll Tasks list found"
                
                #Iterating views
                foreach($view in $list.Views)
                {
                        $view.JSLink = $jSFile
                        $view.Update()
                }
            }
        }
        $web.Dispose()
        

Write-Host "`n"

我哪里错了?任何建议将不胜感激..

使用 for 循环代替 foreach,因为在 foreach 循环内部,无法删除或更新 属性:

       $web = $site.RootWeb
    For($i=0; $i -lt $web.Lists.count; $i++)
    {
       if($web.Lists[$i].Title  -eq $masterTasks)
       {
             write-host "`n`tMaster Tasks list found"

           For($j=0; $j -lt $list.Views.Count; $j++)
           {
                    $view=$list.Views[$j]
                    $view.JSLink  = $jSFile
                    $view.Update()
           }
       }

       if($web.Lists[$i].Title  -eq $allTasks)
        {
            write-host "`n`tAll Tasks list found"
    
            #Iterating views
    
           For($k=0; $k -lt $list.Views.Count; $k++)
           {
                    $otherview= $list.Views[$k]
                    $otherview.JSLink  = $jSFile
                    $otherview.Update()
           }
        }

      
    }
 
    
    $web.Dispose()

这里有一篇博客给出了同样的错误信息和解决方法,请参考:

Collection was modified; enumeration operation may not execute