是否有任何方法可以使用CSOM 来获取Sharepoint页面的Web部件?
我们可以将CSOM与PowerShell脚本一起使用来实现它。
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
}
}
}
参考: 在SharePoint上将CSOM与PowerShell一起使用时从页面检索Webparts
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);
}
}
}
}
如果要从现代页面中获取Webpart,可以使用OfficeDevPnP.Core来实现。以下文章供您参考。
使用PnP Core从SharePoint现代页面获取所有客户端WebPart /如何使用PnP Core提取现代页面WebPart
是的,这很酷...但是可以在C#中实现相同的目标吗???
而且此代码不适用于现代网页
检查上面我更新的回复。
是的,gr8。但是我们不能使用CSOM从现代页面中获取Webparts
据我所知,我们需要使用OfficeDevPnP.Core从现代页面中获取Webpart。