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

Writting logs in Powershell Script

Hello All,

In our project we need to write lots of powershell scripts. So just finding the ways to maintain logs in powershell scripts.

There is a powershell command “Start-Transcript” which allows you to write into a log file.

Start-Transcript <Log File Path>

<Log File Path>: Log file path in which logs are maintained.

After this command we can use either write-output or write-host. Everything used with these commands is get logged into log file.
Whenever if we want to stop the maintain logs there is command “Stop-Transcript”. 


$logFilePath = “c:\logfile.log”  #Log File Path contains folder name and file name.
Start-Transcript $ logFilePath
write-output "Logging Started”

In this way logs are maintained for powershell script.