简体   繁体   中英

Retrieve Sharepoint online groups using Powershell and CSOM

I am trying to set groups Owner of a specific Sub-site using CSOM.

My site collection path is " https://mytenant.sharepoint.com/ ".

Sub-site path " 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 .

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 Server supports two kinds of groups: domain groups and SharePoint groups. Domain groups remain outside SharePoint Server control; users cannot use SharePoint Server to define, browse, or modify domain group membership. SharePoint groups are scoped to the site-collection level, and they can be used only within the site collection. Domain groups can be used anywhere within the scope of the Active Directory directory service.

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. This meams that they were used in some securable object like ListItem .

In SharePoint Client OM this distinction disappeard and you have only Web.SiteGroups collection.

To get Owners, Members and Visitors group for specific web use properties:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM