Is it possible to fetch the content of content editor webpart from SharePoint 2016 using PnP powershell scripting?

Asuri Spandana 20 Reputation points
2023-11-29T07:15:35.0066667+00:00

Hi Team, I am trying to get inner content of content editor webpart from SharePoint 2016 using PnP powershell scripting. But as the content property is not available in Webpart properties. Is their any solution for the above requirement?

SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,296 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,310 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Emily Du-MSFT 44,071 Reputation points Microsoft Vendor
    2023-11-30T06:39:31.9366667+00:00

    Based on my research and testing, we cannot get the Content Editor webpart contents by using PNP PowerShell in the SharePoint 2016.

    You only can use the below PowerShell command to find Content Editor webpart in which site.

    [CmdletBinding()]  
    param([Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$false,HelpMessage="Specifies the URL of the Web Application.")]   
          [string]$WebApplication)  
       
    function Get-CEWP([string]$url)  
    {  
       $manager = $web.GetLimitedWebPartManager($url, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)  
       $webParts = $manager.WebParts  
       if ($webParts.Count -ne 0)  
       {  
          foreach ($webPart in $webParts)  
          {  
             if ($webPart.GetType() -eq [Microsoft.SharePoint.WebPartPages.ContentEditorWebPart])  
             {  
                if ($webPart.ContentLink.Length -gt 0)  
                {  
                   # Check file in ContentLink for script tags  
                   $file = $web.GetFile($webPart.ContentLink)  
                   $data = $file.OpenBinary()  
                   $encode = New-Object System.Text.ASCIIEncoding  
                   $contents = $encode.GetString($data)  
                   if ($contents.ToLower().Contains("<script>"))  
                   {  
                       Write-Output "$($web.Url)/$url (CONTENTLINK)"  
                   }  
                   break  
                }  
       
                if ($webPart.Content.InnerText.Contains("<script>"))  
                {  
                   Write-Output "$($web.Url)/$url (HTML)"  
                }  
             }  
          }  
       }  
    }  
       
    # Load the SharePoint PowerShell snapin if needed  
    if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null)  
    {  
       Write-Host "Loading the SharePoint PowerShell snapin..."  
       Add-PSSnapin Microsoft.SharePoint.PowerShell  
    }     
          
    $SPWebApp = Get-SPWebApplication $WebApplication -EA SilentlyContinue  
    if ($SPWebApp -eq $null)  
    {  
       Write-Error "$WebApplication is not a valid SharePoint Web application. Aborting execution!"  
    }  
    else  
    {  
       Write-Host -ForegroundColor Green "Please wait... gathering data."  
       $sites = $SPWebApp.Sites  
       foreach ($site in $sites)  
       {  
          try  
          {  
             $webs = $site.AllWebs  
             foreach ($web in $webs)  
             {  
                try  
                {  
                   # For publishingwebs, check all publishingpages  
                   if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web))  
                   {  
                      $pubweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)  
                      $pages = $pubweb.GetPublishingPages()  
                      foreach($page in $pages)  
                      {  
                         Get-CEWP -url $page.Url  
                      }  
                   }  
                          
                   # Libraries and lists have views and forms which can contain webparts... let's get them also  
                   $lists = $web.lists  
                   foreach ($list in $lists)  
                   {  
                      # Check the views  
                      $views = $list.Views  
                      foreach ($view in $views)  
                      {  
                         Get-CEWP -url $view.Url  
                      }  
                              
                      # Check the forms  
                      $forms = $list.Forms  
                      foreach ($form in $forms)  
                      {  
                         Get-CEWP -url $form.Url  
                      }  
                   }  
                }  
                catch {}  
                finally { $web.Dispose() }        
             }  
          }  
          catch {}  
          finally { $site.Dispose() }  
       }  
    }
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.