How to Get HTML Content from CEWP using CSOM PowerShell

Sayantan Ganguly 80 Reputation points

Hello Team,
I would like to get all of the html content from each and every webparts situated in all of the pages using SharePoint CSOM PowerShell.

Currently I am getting HTML Content where the webpart resides in default zone. Not getting html content where custom webpart is created. I need to get HTML Content.

Please find the below code:

# Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

# SharePoint Site URL
# SharePoint Online Site URL
$siteUrl = <siteurl>

# SharePoint Online Credentials

# SharePoint Credentials
$credentials = Get-Credential

# Initialize the context
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$ctx.Credentials = New-Object System.Net.NetworkCredential($credentials.UserName, $credentials.Password)

# Web and List Names
$listName = "Pages"  # Replace with your library name

# CAML Query to retrieve all items with .aspx extension
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXml = "<View Scope='RecursiveAll'><Query><Where><And><Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq><Eq><FieldRef Name='File_x0020_Type' /><Value Type='Text'>aspx</Value></Eq></And></Where></Query></View>"

# Get the list
$list = $ctx.Web.Lists.GetByTitle($listName)

# Execute the query
$items = $list.GetItems($query)

# Create an array to store the web part information
$webPartsInfo = @()

# Iterate through each page
foreach ($item in $items) {
    $pageTitle = $item["Title"]
    $pageUrl = $item["FileDirRef"] + "/" + $item["FileLeafRef"]
    # Load the page as a web object
    $web = $ctx.Web
    # Get the page as a file
    $file = $web.GetFileByServerRelativeUrl($item["FileRef"])

    # Get the HTML content from Content Editor Web Part
    # Assuming the CEWP is in the default web part zone
    $webPartManager = $file.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared)

    foreach ($webPart in $webPartManager.WebParts) {
        $webPartTitle = $webPart.WebPart.Title
        $webPartId = $webPart.Id

        # Get HTML content of the web part
        $webPartProperties = $webPart.WebPart.Properties
        $webPartHtmlContent = $webPartProperties.FieldValues["Content"]
        # Create a custom object for the web part information
        $webPartInfo = New-Object PSObject -Property @{
            "PageTitle" = $pageTitle
            "PageURL" = $pageUrl
            "WebPartTitle" = $webPartTitle
            "WebPartID" = $webPartId
            "WebPartHTMLContent" = $webPartHtmlContent

        # Add the web part information to the array
        $webPartsInfo += $webPartInfo

    # Display a progress message
    Write-Host "Processing $pageTitle..."

# Export the web part information to a CSV file
$webPartsInfo | Export-Csv -Path "C:\WebPartsInfo.csv" -NoTypeInformation

# Display a completion message
Write-Host "Web part details exported to CSV."

# Disconnect from the SharePoint site

A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,868 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,715 questions
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,161 questions
0 comments No comments
{count} votes

Accepted answer
  1. ChengFeng - MSFT 5,010 Reputation points Microsoft Vendor

    HI @Sayantan Ganguly

    According to your case description, I do understand how frustrated you are now.

    As far as I know, if you use Powershell CSOM to obtain the content of CEWP (Content Editor Web PART) in the Sharepoint page, you can get HTML content containing OOB (OUT-OF-The-BOX) components。

    The HTML of custom components is because these components are edited by the user and added to CEWP by themselves, and its structure and logo may vary from the project.

    I think this should be a design. Power Shell should be able to perform operations on OOB components, but cannot obtain the content of custom components from PowerShell.

    I think you can put forward your thoughts here :

    Many features of our current products are designed and upgraded based on customers’ feedback. With requirements like this increase, the problem may well be released in the future. Thanks for your understanding.



    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.

    Best Regards

    Cheng Feng

    0 comments No comments

0 additional answers

Sort by: Most helpful