Skip to main content
Added self-running inventory to this script
Source Link
ghangas
  • 1.3k
  • 1
  • 7
  • 25
    $ThisTownIsNotBigEnoughForTheTwoOfUs = ‘http://dev-NotMyCompany’

    ######################## Load Snapin ###################### 
    $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
    if ($snapin -eq $null)
    {  
        Add-PSSnapin "Microsoft.SharePoint.Powershell"
    }
    ######################## End Snapin ########################

    function RADSwap([string]$Url)
    {
        write-host "Processing $Url"
        
        $ParseMe = $Url
        Do 
        {
            $ParseMe = $ParseMe.Remove($ParseMe.LastIndexOf("/"))
            $web = get-spweb $ParseMe -ErrorAction SilentlyContinue
        } while ($web -eq $null)

        $page = $web.GetFile($Url)

        write-host "  Checking out $page from $web"
        
        if ($page.CheckOutType -ne "None")
        {
            try
            {
                $page.UndoCheckOut()
            }
            catch
            {
                $ManualList = $ManualList + $Url
            }
        }
        
        $page.CheckOut();
            
        $wpm = $web$page.GetLimitedWebPartManager($page, "shared");
        $WebParts = $wpm.WebParts;

        write-host "  Iterating through Web Parts"
        for ($i = 0;$webParts.Count -1 ; $i -ltge $webParts.Count;0; $i++$i--)
        {  
        $webPart = $webParts[$i]
            if ($WebPart.RepresentedWebPartType.Name -eq "RadEditorWebPart")
            {
                write-host '    Found RadEditorWebPart. Title:' $webPart.Title '; Zone:' $webPart.ZoneID '; Index:' $webPart.ZoneIndex
                $BlackHat = $webpart
                $Lair = $BlackHat.GetTextForSourceColumn();
                $SixShooter = [xml]$Lair.Replace("BADMARKUP","").trim()
                $HandCuffs = [xml]($SixShooter).BadMarkupData.DesignText
                $DamselInDistress = ($HandCuffs).WebPart.Text.("#text");

                $WhiteHat = new-object Microsoft.SharePoint.WebPartPages.ContentEditorWebPart
                    $WhiteHat.ChromeType = $BlackHat.ChromeType
                    $WhiteHat.Title = $BlackHat($HandCuffs).WebPart.Title
              $WhiteHat.Width = 485
              $WhiteHat.ChromeType = "None"  

                $Horse = New-Object System.Xml.XmlDocument
                $Saddle = $Horse.CreateElement("Content");
                $Saddle.set_InnerText($DamselInDistress);
                $Horse.AppendChild($Saddle);

                $WhiteHat.Content = $Saddle;

                write-host '      Adding new Web Part'
 
                $wpm.AddWebPart($WhiteHat, $BlackHat.ZoneID, $BlackHat.ZoneIndex);

                if ($BlackHat.IsClosed)
                {
                    $wpm.CloseWebPart($wpm.webparts[($wpm.webparts.count-1)])
                    $wpm.SaveChanges($wpm.webparts[($wpm.webparts.count-1)])
                }

                write-host '      Deleting old Web Part'
                $wpm.DeleteWebPart($BlackHat);
            }
        }
        

                write-host '      Checking in'
        $page.CheckIn("Script: Updating the page to replace the TelerikRADEditor with OOTB CEWP.",[Microsoft.SharePoint.SPCheckinType]::MajorCheckIn);
         
        if ($page.Item.ListItems.List.EnableMinorVersions)
        {
                write-host '      Publishing file'
        $page.Publish("Script: Publishing the updated page to replace the TelerikRADEditor with OOTB CEWP.");
        }
        
        if ($page.Item.ListItems.List.EnableModeration)
        {
                write-host '      Approving'
            $page.Approve("Script: Approving the page which has a replaced TelerikRADEditor with OOTB CEWP.");
        }
        
$csv    }


    function enumerateWebParts($Url2) {
    #### Shout out to Ghazwan Khairi for this function ####
    #### http://sharepointquester.com/2012/09/11/list-all-web-parts-in-a-sharepoint-2010-site-collection-using-windows-powershell/ ####
        $site = importnew-csvobject \Documents\SitesToIterateMicrosoft.csvSharePoint.SPSite $Url2    
        foreach($web in $site.AllWebs) {
$csv            if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web)) {
                $pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
                $pages = $pWeb.PagesList
                foreach ($item in $pages.Items) {
                    $fileUrl = $webUrl + “/” + $item.File.Url
                    $manager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
                    $wps = $manager.webparts
                    $wps | %where-object {RADSwap$_.RepresentedWebPartType.Name -eq "RadEditorWebPart"} | select-object @{Expression={$pWeb.Url + $fileUrl};Label=”URL”}
                    
                }
            }
            else {
                $pages = $null
                $pages = $web.Lists["Site Pages"]
                    if ($pages) {
                        foreach ($item in $pages.Items) {
                            $fileUrl = $webUrl + “/” + $item.File.Url
                            $manager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
                            $wps = $manager.webparts
                            $wps | where-object {$_.RepresentedWebPartType.Name -eq "RadEditorWebPart"} | select-object @{Expression={$pWeb.Url + $fileUrl};Label=”URL”}
                    }
                }
                else {
                }
            }        Write-Host “… completed inventorying” $web
        }
    }
    $row = enumerateWebParts($ThisTownIsNotBigEnoughForTheTwoOfUs)
    $row | %{$_.URL.ToString()} | select-object -Unique | %{RADSwap($_)}
function RADSwap([string]$Url)
{
  write-host "Processing $Url"

  $ParseMe = $Url
  Do 
  {
    $ParseMe = $ParseMe.Remove($ParseMe.LastIndexOf("/"))
    $web = get-spweb $ParseMe -ErrorAction SilentlyContinue
  } while ($web -eq $null)

  $page = $web.GetFile($Url)

  write-host "  Checking out $page from $web"

  if ($page.CheckOutType -ne "None")
  {
      $page.UndoCheckOut()
  }

  $page.CheckOut();
    
  $wpm = $web.GetLimitedWebPartManager($page, "shared");
  $WebParts = $wpm.WebParts;

  write-host "  Iterating through Web Parts"
  for ($i = 0; $i -lt $webParts.Count; $i++)
  {  
  $webPart = $webParts[$i]
      if ($WebPart.RepresentedWebPartType.Name -eq "RadEditorWebPart")
      {
          write-host '    Found RadEditorWebPart. Title:' $webPart.Title '; Zone:' $webPart.ZoneID '; Index:' $webPart.ZoneIndex
          $BlackHat = $webpart
          $Lair = $BlackHat.GetTextForSourceColumn();
          $SixShooter = [xml]$Lair.Replace("BADMARKUP","").trim()
          $HandCuffs = [xml]($SixShooter).BadMarkupData.DesignText
          $DamselInDistress = ($HandCuffs).WebPart.Text.("#text");

          $WhiteHat = new-object Microsoft.SharePoint.WebPartPages.ContentEditorWebPart
              $WhiteHat.ChromeType = $BlackHat.ChromeType
              $WhiteHat.Title = $BlackHat.Title
              $WhiteHat.Width = 485
              $WhiteHat.ChromeType = "None"  

          $Horse = New-Object System.Xml.XmlDocument
          $Saddle = $Horse.CreateElement("Content");
          $Saddle.set_InnerText($DamselInDistress);
          $Horse.AppendChild($Saddle);

          $WhiteHat.Content = $Saddle;

          write-host '      Adding new Web Part'
 
          $wpm.AddWebPart($WhiteHat, $BlackHat.ZoneID, $BlackHat.ZoneIndex);

          write-host '      Deleting old Web Part'
          $wpm.DeleteWebPart($BlackHat);
      }
  }
          write-host '      Checking in'
  $page.CheckIn("Script: Updating the page to replace the TelerikRADEditor with OOTB CEWP.",[Microsoft.SharePoint.SPCheckinType]::MajorCheckIn);
          write-host '      Publishing file'
  $page.Publish("Script: Publishing the updated page to replace the TelerikRADEditor with OOTB CEWP.");
          write-host '      Approving'
  $page.Approve("Script: Approving the page which has a replaced TelerikRADEditor with OOTB CEWP.");
}

$csv = import-csv \Documents\SitesToIterate.csv

$csv | % {RADSwap($_.Url)}
    $ThisTownIsNotBigEnoughForTheTwoOfUs = ‘http://dev-NotMyCompany’

    ######################## Load Snapin ###################### 
    $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
    if ($snapin -eq $null)
    {  
        Add-PSSnapin "Microsoft.SharePoint.Powershell"
    }
    ######################## End Snapin ########################

    function RADSwap([string]$Url)
    {
        write-host "Processing $Url"
        
        $ParseMe = $Url
        Do 
        {
            $ParseMe = $ParseMe.Remove($ParseMe.LastIndexOf("/"))
            $web = get-spweb $ParseMe -ErrorAction SilentlyContinue
        } while ($web -eq $null)

        $page = $web.GetFile($Url)

        write-host "  Checking out $page from $web"
        
        if ($page.CheckOutType -ne "None")
        {
            try
            {
                $page.UndoCheckOut()
            }
            catch
            {
                $ManualList = $ManualList + $Url
            }
        }
        
        $page.CheckOut();
            
        $wpm = $page.GetLimitedWebPartManager("shared");
        $WebParts = $wpm.WebParts;

        write-host "  Iterating through Web Parts"
        for ($i = $webParts.Count -1 ; $i -ge 0; $i--)
        {  
        $webPart = $webParts[$i]
            if ($WebPart.RepresentedWebPartType.Name -eq "RadEditorWebPart")
            {
                write-host '    Found RadEditorWebPart. Title:' $webPart.Title '; Zone:' $webPart.ZoneID '; Index:' $webPart.ZoneIndex
                $BlackHat = $webpart
                $Lair = $BlackHat.GetTextForSourceColumn();
                $SixShooter = [xml]$Lair.Replace("BADMARKUP","").trim()
                $HandCuffs = [xml]($SixShooter).BadMarkupData.DesignText
                $DamselInDistress = ($HandCuffs).WebPart.Text.("#text");

                $WhiteHat = new-object Microsoft.SharePoint.WebPartPages.ContentEditorWebPart
                    $WhiteHat.ChromeType = $BlackHat.ChromeType
                    $WhiteHat.Title = ($HandCuffs).WebPart.Title
                    $WhiteHat.ChromeType = "None" 

                $Horse = New-Object System.Xml.XmlDocument
                $Saddle = $Horse.CreateElement("Content");
                $Saddle.set_InnerText($DamselInDistress);
                $Horse.AppendChild($Saddle);

                $WhiteHat.Content = $Saddle;

                write-host '      Adding new Web Part'
                $wpm.AddWebPart($WhiteHat, $BlackHat.ZoneID, $BlackHat.ZoneIndex);

                if ($BlackHat.IsClosed)
                {
                    $wpm.CloseWebPart($wpm.webparts[($wpm.webparts.count-1)])
                    $wpm.SaveChanges($wpm.webparts[($wpm.webparts.count-1)])
                }

                write-host '      Deleting old Web Part'
                $wpm.DeleteWebPart($BlackHat);
            }
        }
        

                write-host '      Checking in'
        $page.CheckIn("Script: Updating the page to replace the TelerikRADEditor with OOTB CEWP.",[Microsoft.SharePoint.SPCheckinType]::MajorCheckIn);
         
        if ($page.Item.ListItems.List.EnableMinorVersions)
        {
                write-host '      Publishing file'
        $page.Publish("Script: Publishing the updated page to replace the TelerikRADEditor with OOTB CEWP.");
        }
        
        if ($page.Item.ListItems.List.EnableModeration)
        {
                write-host '      Approving'
            $page.Approve("Script: Approving the page which has a replaced TelerikRADEditor with OOTB CEWP.");
        }
        
    }


    function enumerateWebParts($Url2) {
    #### Shout out to Ghazwan Khairi for this function ####
    #### http://sharepointquester.com/2012/09/11/list-all-web-parts-in-a-sharepoint-2010-site-collection-using-windows-powershell/ ####
        $site = new-object Microsoft.SharePoint.SPSite $Url2    
        foreach($web in $site.AllWebs) {
            if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web)) {
                $pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
                $pages = $pWeb.PagesList
                foreach ($item in $pages.Items) {
                    $fileUrl = $webUrl + “/” + $item.File.Url
                    $manager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
                    $wps = $manager.webparts
                    $wps | where-object {$_.RepresentedWebPartType.Name -eq "RadEditorWebPart"} | select-object @{Expression={$pWeb.Url + $fileUrl};Label=”URL”}
                    
                }
            }
            else {
                $pages = $null
                $pages = $web.Lists["Site Pages"]
                    if ($pages) {
                        foreach ($item in $pages.Items) {
                            $fileUrl = $webUrl + “/” + $item.File.Url
                            $manager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
                            $wps = $manager.webparts
                            $wps | where-object {$_.RepresentedWebPartType.Name -eq "RadEditorWebPart"} | select-object @{Expression={$pWeb.Url + $fileUrl};Label=”URL”}
                    }
                }
                else {
                }
            }        Write-Host “… completed inventorying” $web
        }
    }
    $row = enumerateWebParts($ThisTownIsNotBigEnoughForTheTwoOfUs)
    $row | %{$_.URL.ToString()} | select-object -Unique | %{RADSwap($_)}
Source Link
ghangas
  • 1.3k
  • 1
  • 7
  • 25

After doing the DB attach I was able to take the content that survived (but throws error) and migrate it to a CEWP like this:

function RADSwap([string]$Url)
{
  write-host "Processing $Url"

  $ParseMe = $Url
  Do 
  {
    $ParseMe = $ParseMe.Remove($ParseMe.LastIndexOf("/"))
    $web = get-spweb $ParseMe -ErrorAction SilentlyContinue
  } while ($web -eq $null)

  $page = $web.GetFile($Url)

  write-host "  Checking out $page from $web"

  if ($page.CheckOutType -ne "None")
  {
      $page.UndoCheckOut()
  }

  $page.CheckOut();
    
  $wpm = $web.GetLimitedWebPartManager($page, "shared");
  $WebParts = $wpm.WebParts;

  write-host "  Iterating through Web Parts"
  for ($i = 0; $i -lt $webParts.Count; $i++)
  {  
  $webPart = $webParts[$i]
      if ($WebPart.RepresentedWebPartType.Name -eq "RadEditorWebPart")
      {
          write-host '    Found RadEditorWebPart. Title:' $webPart.Title '; Zone:' $webPart.ZoneID '; Index:' $webPart.ZoneIndex
          $BlackHat = $webpart
          $Lair = $BlackHat.GetTextForSourceColumn();
          $SixShooter = [xml]$Lair.Replace("BADMARKUP","").trim()
          $HandCuffs = [xml]($SixShooter).BadMarkupData.DesignText
          $DamselInDistress = ($HandCuffs).WebPart.Text.("#text");

          $WhiteHat = new-object Microsoft.SharePoint.WebPartPages.ContentEditorWebPart
              $WhiteHat.ChromeType = $BlackHat.ChromeType
              $WhiteHat.Title = $BlackHat.Title
              $WhiteHat.Width = 485
              $WhiteHat.ChromeType = "None"  

          $Horse = New-Object System.Xml.XmlDocument
          $Saddle = $Horse.CreateElement("Content");
          $Saddle.set_InnerText($DamselInDistress);
          $Horse.AppendChild($Saddle);

          $WhiteHat.Content = $Saddle;

          write-host '      Adding new Web Part'

          $wpm.AddWebPart($WhiteHat, $BlackHat.ZoneID, $BlackHat.ZoneIndex);

          write-host '      Deleting old Web Part'
          $wpm.DeleteWebPart($BlackHat);
      }
  }
          write-host '      Checking in'
  $page.CheckIn("Script: Updating the page to replace the TelerikRADEditor with OOTB CEWP.",[Microsoft.SharePoint.SPCheckinType]::MajorCheckIn);
          write-host '      Publishing file'
  $page.Publish("Script: Publishing the updated page to replace the TelerikRADEditor with OOTB CEWP.");
          write-host '      Approving'
  $page.Approve("Script: Approving the page which has a replaced TelerikRADEditor with OOTB CEWP.");
}

$csv = import-csv \Documents\SitesToIterate.csv

$csv | % {RADSwap($_.Url)}