使用Powershell和CSOM检索Sharepoint在线组

时间:2017-06-20 13:50:33

标签: powershell sharepoint office365 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()  

} 

有谁能解释我做错了什么?

2 个答案:

答案 0 :(得分:2)

SharePoint组的范围限定为网站集(docs):

  

SharePoint Server支持两种组:域组和SharePoint组。域组仍在SharePoint Server控制之外;用户无法使用SharePoint Server定义,浏览或修改域组成员身份。 SharePoint组的范围限定为网站集级别,它们只能在网站集中使用。域组可以在Active Directory目录服务范围内的任何位置使用。

在SharePoint Server OM中,您有SPWeb.SiteGroupsSPWeb.Groups,第二个允许您自动筛选出在特定Web的自定义权限中使用的组。这些都是在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  

使用此功能进一步参考:Using the Client Object Model

答案 1 :(得分:0)

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;
}