Is there any way out to fetch the webparts of a Sharepoint page using CSOM?
We can use CSOM with PowerShell script to achieve it.
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"
$siteURL = "http://sp2013/sites/team"
$username="admin"
$password="password"
$domain="contoso"
#page to be viewed
$pageRelativeUrl = "/sites/team/SitePages/WP20200203.aspx"
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
$ctx.credentials=New-Object System.Net.NetworkCredential($username, $password, $domain);
#Get the page
$file = $ctx.Web.GetFileByServerRelativeUrl($pageRelativeUrl)
$ctx.Load($file)
$ctx.ExecuteQuery()
#Get all the webparts
Write-Host "Retrieving webparts"
$wpManager = $file.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared)
$webparts = $wpManager.Webparts
$ctx.Load($webparts)
$ctx.ExecuteQuery()
if($webparts.Count -gt 0){
Write-Host "Looping through all webparts"
foreach($webpart in $webparts){
$ctx.Load($webpart.WebPart.Properties)
$ctx.ExecuteQuery()
$propValues = $webpart.WebPart.Properties.FieldValues
Write-Host "ID: $webpart.id"
foreach($property in $propValues){
Write-Host "Title: " $property.Title
Write-Host "Description: " $property.Description
Write-Host "Chrome Type: " $property.ChromeType
}
}
}
Reference: Retrieve Webparts From Page Using CSOM With PowerShell On SharePoint
C# code:
var siteURL = "http://sp2013/sites/team";
var username="admin";
var password="password";
var domain="contoso";
var pageRelativeUrl = "/sites/team/SitePages/WP20200203.aspx";
var ctx = new Microsoft.SharePoint.Client.ClientContext(siteURL);
ctx.Credentials=new System.Net.NetworkCredential(username, password, domain);
var file = ctx.Web.GetFileByServerRelativeUrl(pageRelativeUrl);
ctx.Load(file);
ctx.ExecuteQuery();
var wpManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
var webparts = wpManager.WebParts;
ctx.Load(webparts);
ctx.ExecuteQuery();
if(webparts.Count>0){
foreach(var webpart in webparts){
ctx.Load(webpart.WebPart.Properties);
ctx.ExecuteQuery();
var propValues = webpart.WebPart.Properties.FieldValues;
foreach(var property in propValues){
if (property.Key == "Title")
{
Console.WriteLine("Title: " + property.Value);
}
if (property.Key == "Description")
{
Console.WriteLine("Description: " + property.Value);
}
}
}
}
If you want to get webparts from the modern page, we can use OfficeDevPnP.Core to achieve it. The following article for your reference.
Ya that's cool...but can the same be achieved in c#???
And this code is not working for modern web pages
Check my updated reply above.
Ya that's gr8. But can't we get webparts from the modern pages using CSOM
Per my knowledge, we need use OfficeDevPnP.Core to get webparts from the modern pages.