【问题标题】:Retrieve Sharepoint online groups using Powershell and CSOM使用 Powershell 和 CSOM 检索 Sharepoint 在线组
【发布时间】:2017-11-23 02:40:21
【问题描述】:

我正在尝试使用 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()  

} 

谁能解释我做错了什么?

【问题讨论】:

    标签: powershell sharepoint office365 csom


    【解决方案1】:

    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.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

    【讨论】:

    • 我试过 $web.AssociatedVisitorGroup 还是不行
    • 它检索我:Microsoft.SharePoint.Client.Group
    • 哦,好的,使用:$web.AssociatedVisitorGroup.Title 显示组显示名称
    • 它适用于网站集,但不适用于子网站错误:Exception lors de l'appel de «ExecuteQuery» avec «0» 参数:«无法调用方法或从空对象检索属性。以下调用堆栈返回的对象为空。 "AssociatedVisitorGroup Web Microsoft.SharePoint.SPContext.Current
    • 您的脚本在网站集上工作正常,我在子网站上没有结果
    【解决方案2】:

    下面的代码对我有用

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-13
      • 2018-01-03
      • 2019-04-12
      • 2023-03-09
      • 2023-03-19
      • 1970-01-01
      • 2020-08-10
      • 2017-02-08
      相关资源
      最近更新 更多