‎get html content from cewp using powershell csom‎

Sayantan Ganguly 80 Reputation points
2023-10-09T13:43:49.6233333+00:00
Hello, I am able to get HTML Content from CEWP using SharePoint CSOM POwerShell. Below is my sample code:
$webPartManager = $file.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared)
    $ctx.Load($webPartManager.WebParts)
    $ctx.ExecuteQuery()

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

        # Get HTML content of the web part
        $webPartProperties = $webPart.WebPart.Properties
        $ctx.Load($webPartProperties)
        $ctx.ExecuteQuery()
        $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
        }


But issue is when the webpart is not stored in default webpart zone I am not able to get HTML Content.

Is there anyway to get the html content for all of the pages?
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,792 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.
3,027 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,575 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. ChengFeng - MSFT 5,025 Reputation points Microsoft Vendor
    2023-10-10T06:33:07.29+00:00

    Hi @Sayantan Ganguly

    Judging from your code, what you get should be the content in the same page. If so, you should get the Webpart control contained in the corresponding page.

    Have you tried traversing all pages, getting the Webpart content contained in each page, and getting all the pages you need to get?

    The following is a reference code. The specific situation needs to be adjusted according to your environment.

    # Initialize the context
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
    $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
    
    # Get all pages in the site
    $pagesList = $ctx.Web.Lists.GetByTitle("Site Pages")
    $query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
    $items = $pagesList.GetItems($query)
    
    $ctx.Load($items)
    $ctx.ExecuteQuery()
    
    # Iterate through each page
    foreach ($item in $items) {
        $pageUrl = $item["FileRef"]
        $file = $ctx.Web.GetFileByServerRelativeUrl($pageUrl)
        $ctx.Load($file)
    
        # Get web parts on the current page
        $webPartManager = $file.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared)
        $ctx.Load($webPartManager.WebParts)
        $ctx.ExecuteQuery()
    
        foreach ($webPart in $webPartManager.WebParts) {
            $webPartTitle = $webPart.WebPart.Title
            $webPartId = $webPart.Id
    
            # Get HTML content of the web part
            $webPartProperties = $webPart.WebPart.Properties
            $ctx.Load($webPartProperties)
            $ctx.ExecuteQuery()
            $webPartHtmlContent = $webPartProperties.FieldValues["Content"]
    
            # Create a custom object for the web part information
            $webPartInfo = New-Object PSObject -Property @{
                "PageURL" = $pageUrl
                "WebPartTitle" = $webPartTitle
                "WebPartID" = $webPartId
                "WebPartHTMLContent" = $webPartHtmlContent
            }
    
            # Output or process $webPartInfo as needed
            Write-Host "Page URL: $($webPartInfo.PageURL)"
            Write-Host "Web Part Title: $($webPartInfo.WebPartTitle)"
            Write-Host "Web Part ID: $($webPartInfo.WebPartID)"
            Write-Host "Web Part HTML Content: $($webPartInfo.WebPartHTMLContent)"
        }
    }
    

    ***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


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.