[英]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.SiteGroups
和SPWeb.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.