0

following is my code:

foreach($field in $list.Fields)
{
    if($field.StaticName -eq "temp1" -or $field.StaticName -eq "temp2")
     { 
         if($field.Id -ne $depId)
         {
             $field.AllowDeletion = $true
             $field.Delete()
             $list.Update()
         }
     }
     else 
     {
         if($field.StaticName -eq "notActive" -and $field.Id -ne $aktId)
         {
              $field.AllowDeletion = $true
              $field.Delete()
              $list.Update()
         }
     }
  }

but it generates, Collection was modified; enumeration operation may not execute.

3

You can not modify size of collection that is used by foreach operator. You can save ids of field that should be deleted then loop through the ids, find this field in field collection by id and remove it.

  • thanks, yes you are right, and I need to use for-loop insead of foreach. – Medes Apr 3 '12 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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