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

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.

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
thanks, yes you are right, and I need to use for-loop insead of foreach. –  Medes Apr 3 '12 at 17:51
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.