Change Content of Content Editor WebPart Using PowerShell SharePoint 2013

This PowerShell script will change the content of a content editor webpart based on its title. You can change the title of target webpart as well as in PowerShell script too.

1. Site Collections File

Create text file with name “SiteCollections.txt” in the same directory where script file is present.
Write site collections urls in “SiteCollections” file.
Make it sure each url should be in new line and write “End Point” at the end as shown below. End Point is a reserve word for this script.



  2. Open SharePoint 2013 Management Shell




3.   Change directory path where script file is saved.




4.    Type script name

Copy and Save this script with name like ChangeWebPartContent.ps1 and ps1 extention.
Type ”./ ChangeWebPartContent.ps1 –path “directory path\SiteCollections.txt” –overwrite o “ and press enter key.
Or
Type ”./ ChangeWebPartContent.ps1 –path “directory path\SiteCollections.txt” –overwrite A “ and press enter key.
-overwrite o will over write web part content and –overwrite a will append to web part content. 

param([string]$Path = "", [string]$Overwrite = "")

Function ChangeWebPartContent([string]$SPSite, [string]$OverWriteContent)
{
    #Write-Host "Site : " $SPSite "  Overwrite : " $OverWriteContent
          $site=Get-SPSite $SPSite -ErrorAction SilentlyContinue
         if($site -ne $null)
          {Write-Host "Web Found: " $site.RootWeb.URL}
          else{             
             [console]::ForegroundColor = "Red"
            write-Host "Web Not Found: " $SPSite
            [console]::ForegroundColor = "White"
          }


    #Loop throgh each subsite in the site collection
    try{
        $file= $web.GetFile($site.RootWeb.RootFolder.WelcomePage)
         #Write-Host "Found Welcome Page: " $web.URL
        }
    catch [System.Exception] 
        {
            #Write-Host $_.Exception.Message
            #Write-Host "Unable to get Welcome Page: " $web.URL
        }

    foreach($web in $Site.RootWeb)
    {
          #Get the Default.aspx file
          try{
          $file= $web.GetFile($web.Url +$web.RootFolder.WelcomePage)
          }
          catch
          {
          }
          #Write-Host $file.URL
 
          if($file.Exists)
          {
         #Web Part Manager to get all web parts from the file
         $WebPartManager = $web.GetLimitedWebPartManager( $file,  [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
  
         #Iterate through each web part
         foreach($webPart in $WebPartManager.WebParts)
          {
            # Get the Content Editor web part with specific Title
            if( ($webPart.title -eq "My Custom Web Part" -Or $webPart.title -eq "Target Web Part Title" -and ($webPart.GetType() -eq [Microsoft.SharePoint.WebPartPages.ContentEditorWebPart]) )
            {
            try{
               #Content to be Placed inside CEWP
               $HtmlContent= "Hello, this is updated content."
 
               $XmlDoc = New-Object System.Xml.XmlDocument
               $contentXml=$xmlDoc.CreateElement("content")
                if($OverWriteContent -eq "A")
                   {
                       $HtmlContent = $webpart.Content.InnerText + $HtmlContent
                   }
               $contentXml.InnerText = $HtmlContent
 
               #Set content and Save
               $webpart.Content = $contentXml    
               $webPartManager.SaveChanges($webPart);
               [console]::ForegroundColor = "Green"
                Write-Host "Webpart found: " $web.URL
                [console]::ForegroundColor = "White"
               }
               catch [System.Exception] 
                {
                    #Write-Host $_.Exception.Message
                    [console]::ForegroundColor = "Red"
                    Write-Host "Unable to update webpart: " $web.URL
                    [console]::ForegroundColor = "White"
                }
             }
          }
        }
     }
 }




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

$SitesArray =  Get-Content -Path ($ScriptPath)
$OverWriteData = $OverWrite
     if($OverWriteData -eq "O" -Or $OverWriteData -eq "A")
        {
            for($i=0; $i -le $SitesArray.Length; $i++)
            {
                if($SitesArray[$i] -ne "End Point" -And $SitesArray[$i].Length -gt "5")
                {
                    ChangeWebPartContent $SitesArray[$i] $OverWrite
                }
            }
        }
     else
        {
            [console]::ForegroundColor = "Red"
            Write-Host "Unknown input, try again...!"
            [console]::ForegroundColor = "White"
        }

Comments

Post a Comment

Popular Posts

GREYCstoration Oil Paint plugin for Photoshop

Service Bus Gateway service stuck at Starting

PowerApps SubmitForm not clearing People Picker value

Apple iPhone sending SMS automatically 00447786205094

Download file failed signature verification and may have been tampered with - Workflow Manger 1.0 Refresh (CU2)