RepresentedWebPartType or ErrorWebPart Problem in SharePoint

Hello,

Today new problem and solution.

Problem

In our project, we have to delete the webpart using powershell script. I am passing the webpart type name as a parameter and retrieving through all webparts and comparing the webpart type name with the passed type name. But it seems that this condition never worked. After digging into more details it found that powershell always showing the our webpart type name as ErrorWebPart(Microsoft.SharePoint.WebPartPages.ErrorWebPart). Also this webpart appears correctly on the page and also it shows the correct type on the page when I check by URL?Contents=1.

Solution

Our webpart is ContentQuery webpart and we have deleted the Item Style for this webpart (Since we have to delete the Content Query webpart itself) from the ItemStyle.xsl and that’s why it was shown as ErrorWebPart. The Error webpart is placeholder for the webparts which have some problems.

The solution to this problem is to use RepresentedWebPartType property of ErrorWebpart. It shows the correct type. The webpart which works correctly for them value of RepresentedWebPartType is Null.

So my condition became:

  1. $WebPartCollection = $webpartmanager.WebParts  
  2. for($i=0;$i -lt $WebPartCollection.Count; $i++){  
  3.   
  4.    if($webpartmanager.Webparts[$i].RepresentedWebPartType.Name -eq $webPartName){  
  5.       #delete webpart code  
  6.    }  

Where

  1. $webpartmanager = Webpart Manager obtained from the current page  
  2. $webPartName = Webpart which I want to delete 

Thanks!

Enjoy reading.

Ebook Download
View all
Learn
View all