2017-06-20 31 views
0

我想使用CSOM设置特定子站点的组所有者。使用Powershell和CSOM检索Sharepoint联机组

我的网站收藏路径是“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

SharePoint组的作用域网站集(docs):

SharePoint服务器支持两种群体:域组和SharePoint组。域组保留在SharePoint Server控制之外;用户无法使用SharePoint Server来定义,浏览或修改域组成员资格。 SharePoint组的作用域为网站集级别,并且只能在网站集内使用。域组可以在Active Directory目录服务范围内的任何位置使用。

在SharePoint Server OM你有SPWeb.SiteGroupsSPWeb.Groups,第二可以自动过滤掉特定的网页自定义权限中使用的群体。这个meams,他们被用在一些安全的对象,如ListItem

在SharePoint客户端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

+0

我尝试$ web.AssociatedVisitorGroup,仍然不起作用 – ysfibm

+0

你是什么意思?你得到错误,属性为空? – tinamou

+0

它检索我:Microsoft.SharePoint.Client.Group – ysfibm

0

下面的代码为我工作

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; 
} 
相关问题