Friday, 2 March 2012

RepresentedWebPartType or ErrorWebPart



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.
Our webpart is ContentQuery webpart and we have deleted the ItemStyle 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

for($i=0;$i -lt $WebPartCollection.Count; $i++){

$WebPartCollection = $webpartmanager.WebParts
if($webpartmanager.Webparts[$i].RepresentedWebPartType.Name -eq $webPartName){
       #delete webpart code


$webpartmanager = Webpart Manager obtained from the current page


Enjoy reading J

1 comment:

lee woo said...

Women should never go without earrings. Passing on them is an opportunity missed. See the link below for more info.