question

DevP-3568 avatar image
0 Votes"
DevP-3568 asked DevP-3568 commented

updating List views SharePoint PowerShell

I want to be able to update the "JSLink" property on all views in two different lists. However I keep getting the following error:

"Collection was modified; enumeration operation may not execute."

I have the following 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"
office-sharepoint-server-development
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MichaelHan-MSFT avatar image
1 Vote"
MichaelHan-MSFT answered DevP-3568 commented

Hi @DevP-3568,

It's not supported to use add/delete/update method inside foreach loop, otherwise you would get this error.

You need to use for loop instead to achieve this:


   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($j=0; $j -lt $list.Views.Count; $j++)
            {
                     $view= $list.Views[$k]
                     $view.JSLink  = $jSFile
                     $view.Update()
            }
         }     
     }


If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

that did the trick. many thanks..

0 Votes 0 ·