簡體   English   中英

使用Powershell和CSOM檢索Sharepoint在線組

[英]Retrieve Sharepoint online groups using Powershell and CSOM

我正在嘗試使用CSOM設置特定子站點的組所有者。

我的網站集路徑為“ https://mytenant.sharepoint.com/ ”。

子站點路徑“ https://mytenant.sharepoint.com/subsite ”。

首先,我想檢索特定子站點的SharePoint組,但是我的代碼將檢索集合站點的所有子站點組。

這是我的代碼:

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"  
$siteURL = "https://mytenant.sharepoint.com/subsite"  
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
$userId = "myID"  
$pwd = Read-Host -Prompt "Enter password" -AsSecureString  
$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  
$ctx.credentials = $creds   

$web = $ctx.web  
$groups = $web.SiteGroups 
$ctx.Load($groups)  

$ctx.ExecuteQuery() 

foreach($group in $groups){  
    Write-Host "Site Group : " $group.Title  
    $ctx.ExecuteQuery()  

} 

誰能解釋我在做什么錯?

SharePoint組的作用域是網站集( docs ):

SharePoint Server支持兩種類型的組:域組和SharePoint組。 域組仍然不受SharePoint Server的控制; 用戶不能使用SharePoint Server定義,瀏覽或修改域組成員身份。 SharePoint組的作用域是網站集級別,並且只能在網站集內使用。 域組可以在Active Directory目錄服務范圍內的任何位置使用。

在SharePoint Server OM中,您具有SPWeb.SiteGroupsSPWeb.Groups ,第二個功能使您可以自動篩選出特定網站的自定義權限中使用的組。 這意味着它們已用於某些安全對象(如ListItem

在SharePoint Client OM中,這種區別消失了,並且只有Web.SiteGroups集合。

要獲得所有者,成員和訪客組的特定Web使用屬性:

$web.AssociatedVisitorGroup
$web.AssociatedMemberGroup
$web.AssociatedOwnerGroup

您可以使用方法創建它們:

$usr = $web.EnsureUser($userId)
$ctx.Load($usr)
$ctx.ExecuteQuery()
$web.CreateDefaultAssociatedGroups($usr.LoginName, "", "")
$ctx.ExecuteQuery()

更新后的腳本應如下所示:

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"  
$siteURL = "https://mytenant.sharepoint.com/subsite"  
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
$userId = "myID"  
$pwd = Read-Host -Prompt "Enter password" -AsSecureString  
$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  
$ctx.credentials = $creds   

$web = $ctx.web

$ctx.Load($web.AssociatedMemberGroup)
$ctx.Load($web.AssociatedOwnerGroup)
$ctx.Load($web.AssociatedVisitorGroup)
$ctx.ExecuteQuery() 

Write-Host "Owners group : " $web.AssociatedMemberGroup.Title  
Write-Host "Members group : " $web.AssociatedMemberGroup.Title  
Write-Host "Visitors group : " $web.AssociatedMemberGroup.Title  

使用此作為進一步參考: 使用客戶端對象模型

下面的代碼為我工作

 ClientContext _ctx= new ClientContext(strURL);  
_ctx.Credentials = new NetworkCredential(strUserName, pass); 
Web web = _ctx.Site.RootWeb;
_ctx.Load(web, x => x.Title);
var groups = web.SiteGroups;
_ctx.Load(web.SiteGroups);
_ctx.ExecuteQuery();

string strGroup = "";
foreach (var grp in groups)
{
      strGroup += grp.Title + " : " + grp.Id;
}  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM