简体   繁体   English

使用Powershell和CSOM检索Sharepoint在线组

[英]Retrieve Sharepoint online groups using Powershell and CSOM

I am trying to set groups Owner of a specific Sub-site using CSOM. 我正在尝试使用CSOM设置特定子站点的组所有者。

My site collection path is " https://mytenant.sharepoint.com/ ". 我的网站集路径为“ https://mytenant.sharepoint.com/ ”。

Sub-site path " https://mytenant.sharepoint.com/subsite ". 子站点路径“ https://mytenant.sharepoint.com/subsite ”。

First I wanna retrieve SharePoint Groups of my specific Sub-site but my code retrieve all Sub-sites groups of my collection site . 首先,我想检索特定子站点的SharePoint组,但是我的代码将检索集合站点的所有子站点组。

Here's my code : 这是我的代码:

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()  

} 

Can anyone explain what I'm doing wrong ? 谁能解释我在做什么错?

SharePoint groups are scoped to site collection ( docs ): SharePoint组的作用域是网站集( docs ):

SharePoint Server supports two kinds of groups: domain groups and SharePoint groups. SharePoint Server支持两种类型的组:域组和SharePoint组。 Domain groups remain outside SharePoint Server control; 域组仍然不受SharePoint Server的控制; users cannot use SharePoint Server to define, browse, or modify domain group membership. 用户不能使用SharePoint Server定义,浏览或修改域组成员身份。 SharePoint groups are scoped to the site-collection level, and they can be used only within the site collection. SharePoint组的作用域是网站集级别,并且只能在网站集内使用。 Domain groups can be used anywhere within the scope of the Active Directory directory service. 域组可以在Active Directory目录服务范围内的任何位置使用。

In SharePoint Server OM you have SPWeb.SiteGroups and SPWeb.Groups , second allows you to automatically filter out groups that were used in custom permissions for specific web. 在SharePoint Server OM中,您具有SPWeb.SiteGroupsSPWeb.Groups ,第二个功能使您可以自动筛选出特定网站的自定义权限中使用的组。 This meams that they were used in some securable object like ListItem . 这意味着它们已用于某些安全对象(如ListItem

In SharePoint Client OM this distinction disappeard and you have only Web.SiteGroups collection. 在SharePoint Client OM中,这种区别消失了,并且只有Web.SiteGroups集合。

To get Owners, Members and Visitors group for specific web use properties: 要获得所有者,成员和访客组的特定Web使用属性:

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

You can create them with method: 您可以使用方法创建它们:

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

Your updated script should look like this: 更新后的脚本应如下所示:

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  

Use this for further reference: Using the Client Object Model 使用此作为进一步参考: 使用客户端对象模型

below code worked for me 下面的代码为我工作

 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