Replace Web part Instances in sharepoint Using Powershell

 

####Parameters

$SqlServerInstance =”SQL Instance Name”

 
 

#############

 
 

Start-Transcript -Append

#region environment and deploy packages

Write-Host “$(Get-Date -Format o) Getting Environment –”

 
 

$SiteUrl=”http://localhost”

 
 

<#

if($args[0] -eq ‘Dev’) {

$SiteUrl=”http://localhost”

}

elseif ($args[0] -eq ‘QA’) {

$SiteUrl=”http://localhost”

}

elseif ($args[0] -eq ‘Prod’) {

$SiteUrl=”http://localhost”

}

else {

Write-Host “$(Get-Date -Format o) Environment not specified – please pass appropriate environment to deploy -”

exit

}

 
 

if($SiteUrl -eq ”)

{

Write-Host “$(Get-Date -Format o) Incorrect Environment specified – please pass ‘Dev/QA/Prod’ -”

exit

}

#>

 
 

$powershellSnapin=”Microsoft.SharePoint.Powershell”

if ((Get-PSSnapin -Name $powershellSnapin -ErrorAction SilentlyContinue) -eq $null )

{

Write-Host “——————————— Adding sharpoint powershell snapin ————————-”

Add-PsSnapin $powershellSnapin

}

 
 

$SQLSnapin = “SqlServerCmdletSnapin100”

 
 

if ((Get-PSSnapin -Name $SQLSnapin -ErrorAction SilentlyContinue) -eq $null )

{

Write-Host “——————————— Adding SQL powershell snapin ————————-”

Add-PsSnapin $SQLSnapin

}

 
 

$SQLProviderSnapin = “SqlServerProviderSnapin100”

 
 

if ((Get-PSSnapin -Name $SQLProviderSnapin -ErrorAction SilentlyContinue) -eq $null )

{

Write-Host “——————————— Adding SQL Provider Snapin ————————-”

Add-PsSnapin $SQLProviderSnapin

}

 
 

 
 

# —————- Find Existing Pages —————-

 
 

Function FindExistingWebpartReferences([string]$WebpartID)

{

 
 

$DBName= Get-SPContentDatabase -WebApplication $SiteUrl

write-host $DBName.Name

 
 

foreach ($element in $DBName.Name)

{

write-host “Looping through ” $element

Invoke-Sqlcmd -Query “USE $element;select distinct         d.SiteId,D.WebId, D.Id As DocumentId, D.DirName, D.LeafName, tp_ID As

 
 

WebPartSK,w.Title as SiteTitle FROM Docs D WITH (nolock) INNER JOIN Webs W WITH (nolock) ON D.WebID = W.Id INNER JOIN WebParts WP WITH (nolock) ON D.Id =

 
 

WP.tp_PageUrlID WHERE WP.tp_ListId Is Null AND WP.tp_Type Is Null AND WP.tp_Flags Is Null AND WP.tp_BaseViewID Is Null AND WP.tp_DisplayName Is Null

 
 

AND WP.tp_WebPartTypeId=’$WebpartID’; ” -ServerInstance $SqlServerInstance

}

 
 

}

#———————- Replace Exisiting Ones ——————-

 
 

Function ReplaceWebpartReferences([string]$OldWebpartID,[string]$NewWebpartID)

{

$DBName= Get-SPContentDatabase -WebApplication $SiteUrl

write-host $DBName.Name

 
 

foreach ($element in $DBName.Name)

{

write-host “Looping through ” $element

Invoke-Sqlcmd -Query “USE $element;DECLARE @OldWebPartTypeId uniqueidentifier;DECLARE @NewWebPartTypeId uniqueidentifier; SET @OldWebPartTypeId = ‘$OldWebpartID’; SET @NewWebPartTypeId = ‘$NewWebpartID’; UPDATE [WebParts] SET [tp_WebPartTypeId] = @NewWebPartTypeId ;” –

 
 

ServerInstance $SqlServerInstance

}

}

 
 

#######execute#######

 
 

FindExistingWebpartReferences “GUID of exisitng web part instance”

#ReplaceWebpartReferences “GUID of exisitng web part instance”,”GUID of New Web part ID”

 
 

#####################

 
 

Stop-Transcript

 
 

Write-Host “——————————— Remove SQL Snapin ————————-”

Remove-PsSnapin “SqlServerCmdletSnapin100”

Remove-PsSnapin “SqlServerProviderSnapin100”

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami