使用 CSOM 在 Sharepoint 中获取页面的 Web 部件
Fetch webparts of a page in Sharepoint using CSOM
是否有任何方法可以使用 CSOM 获取 Sharepoint 页面的 webparts?
我们可以使用 CSOM 和 PowerShell 脚本来实现它。
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\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
}
}
}
参考:Retrieve Webparts From Page Using CSOM With PowerShell On SharePoint
C#代码:
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);
}
}
}
}
如果想从现代页面获取webparts,我们可以使用OfficeDevPnP.Core来实现。以下文章供大家参考。
是否有任何方法可以使用 CSOM 获取 Sharepoint 页面的 webparts?
我们可以使用 CSOM 和 PowerShell 脚本来实现它。
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\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
}
}
}
参考:Retrieve Webparts From Page Using CSOM With PowerShell On SharePoint
C#代码:
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);
}
}
}
}
如果想从现代页面获取webparts,我们可以使用OfficeDevPnP.Core来实现。以下文章供大家参考。