Friday, 2 March 2012

RepresentedWebPartType or ErrorWebPart


 Hello,

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 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
}
}

Where

$webpartmanager = Webpart Manager obtained from the current page


Thanks!

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.
Syntax:

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”. 

Example:

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

In this way logs are maintained for powershell script.

Thanks!

Br,
Prasham