Получить веб-части страницы в Sharepoint с помощью CSOM - PullRequest
0 голосов
/ 03 февраля 2020

Есть ли какой-нибудь способ получить веб-части страницы Sharepoint с помощью CSOM?

1 Ответ

1 голос
/ 03 февраля 2020

Мы можем использовать 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  
        }             
    }  
}  

Ссылка : Получение веб-частей со страницы с использованием CSOM с PowerShell в 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);
            }

        }             
    }  
} 

Если вы хотите получать веб-части с современной страницы, мы можем использовать OfficeDevPnP.Core для достижения этого. Следующая статья для справки.

Получить все клиентские веб-части с современной страницы SharePoint с использованием PnP Core / Как извлечь современные веб-части с использованием PnP Core

...