0

I am attempting to delete some webparts from various pages in a site collection using Powershell. It seems that 'querying' the webparts on the page pulls the correct number of webparts to delete, but when I go to actually delete them (by uncommenting the appropriate line below), it will 'skip' over the second web part and jump out of the loop.

The code below is specific to my case:

#Get reference to the webpartmanager class  
$webpartmanager = $SPWeb.GetLimitedWebPartManager($pageitem.url,[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)    

#Iterate through the WebParts on the page
for($i=0;$i -lt $webpartmanager.WebParts.Count;$i++)
{
  write-host $i
  #Get reference to the web part  
  $wp=$webpartmanager.WebParts[$i];
  write-host $("`t"+$wp.GetType().ToString()) -foregroundcolor "darkblue"

  #Iterate WebParts
  if($wp.GetType().ToString() -like "*OurNHLinksRollup*")
  {                                   
    write-host $("`t"+$wp.Title) -foregroundcolor "White"   
    write-host $("`t"+$wp.GetType().ToString()) -foregroundcolor "Gray"
    #Remove the Web part from the page
    #$webpartmanager.DeleteWebPart($webpartmanager.WebParts[$wp.ID])
    write-host "`t`tDeleted." -foregroundcolor "Red"
  }
  elseif($wp.GetType().ToString() -like "*OurNHDocumentRollup*")
  {                                   
    write-host $("`t"+$wp.Title) -foregroundcolor "White"
    write-host $("`t"+$wp.GetType().ToString()) -foregroundcolor "Gray"
    #Remove the Web part from the page
    #$webpartmanager.DeleteWebPart($webpartmanager.WebParts[$wp.ID])
    write-host "`t`tDeleted." -foregroundcolor "Red"
  }
}

It is very odd that it will write to screen all webparts I am attempting to delete when I have the actual "DeleteWebPart" line commented out, but when I actually uncomment this line to delete the items, it will only delete a single web part and move out of the loop.
Here is the screen output with the delete lines commented out:

    : test

    (CheckOut):     test

0

    NH.Webparts.Webparts.OurNHLinksRollup.OurNHLinksRollup

    Links

    NH.Webparts.Webparts.OurNHLinksRollup.OurNHLinksRollup

            Deleted.

1

    NH.Webparts.Webparts.OurNHDocumentRollup.OurNHDocumentRollup

    Documents

    NH.Webparts.Webparts.OurNHDocumentRollup.OurNHDocumentRollup

            Deleted.

...And with the DELETE line uncommented:

     : test

    (CheckOut):     test

0

    NH.Webparts.Webparts.OurNHLinksRollup.OurNHLinksRollup

    Links

    NH.Webparts.Webparts.OurNHLinksRollup.OurNHLinksRollup

            Deleted.

    (CheckIn):      test

Any idea why this is occurring?

2

This might be caused by the way you are looping over the web parts. You are starting at item 0 and counting up, deleting things as you find them. You need to start at the highest number in the collection and count down.

Sharepoint moves the web part indices up after you delete an item, effectively skipping what should be your next web part.

0

I figured out a different method. Using the Where-Object filter in my foreach statement seems to work better. The following works every time:

  #Get reference to the webpartmanager class  
  $webpartmanager = $SPWeb.GetLimitedWebPartManager($pageitem.url,[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)    

  #Iterate through the WebParts on the page
  foreach($wp in $webpartmanager.WebParts | Where-Object {($_.GetType().ToString() -like "*OurNHLinksRollup*") -or ($_.GetType().ToString() -like "*OurNHDocumentRollup*")})
  {
    write-host $("`t"+$wp.Title) -foregroundcolor "White"   
    write-host $("`t"+$wp.GetType().ToString()) -foregroundcolor "Gray"

    #Remove the Web part from the page
    $webpartmanager.DeleteWebPart($webpartmanager.WebParts[$wp.ID])
    write-host "`t`tDeleted." -foregroundcolor "Red"
  }

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.