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