温馨提示:本文翻译自stackoverflow.com,查看原文请点击:microsoft graph - Fetch webparts of a page in Sharepoint using CSOM
microsoft-graph sharepoint sharepoint-2013

microsoft graph - 使用CSOM在Sharepoint中获取页面的Web部件

发布于 2020-04-13 17:41:52

是否有任何方法可以使用CSOM 来获取Sharepoint页面Web部件

查看更多

提问者
Rohit Kumar
被浏览
54
LZ_MSFT 2020-02-04 15:26

我们可以将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