1

In SPO we have a couple of sites that lack a default group for the "Allow members to share this site and individual files and folders".

enter image description here

I'm trying to set a SharePoint security group named "OOTB Visitors" as the default group for this through PowerShell.

Through the GUI it's easily done by going into the group choosing:

enter image description here

In PowerShell I found a method for the web named .CreateDefaultAssociatedGroups()

However, I can't get it to work. I don't get an error message but it also doesn't change anything.

Any idea what I'm missing?

Here's my code:

$ctx = Get-PnPContext
$web.CreateDefaultAssociatedGroups('Owners', $null, 'OOTB Visitors')
$web.Update()
$ctx.ExecuteQuery()
1

Please try the following code:

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  

$siteURL = "https://xxx.sharepoint.com/sites/wendy2"  
$userId = "xxx@xxx.onmicrosoft.com"  
$pwd = Read-Host -Prompt "Enter password" -AsSecureString  
$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  
$ctx.credentials = $creds  
try{  
    $web = $ctx.web  
    $ctx.load($web) 
    $ctx.executeQuery()  
    $web.CreateDefaultAssociatedGroups('xxx', $null, 'xxx')
    $web.Update()
    $ctx.executeQuery()
}  
catch{  
    write-host "$($_.Exception.Message)" -foregroundcolor red  
} 
  • Thanks. Almost there but no success yet. I believe it must have something to do with the arguments passed to the CreateDefaultAssociatedGroups method... – colonel_claypoo Nov 28 '18 at 8:11
0

I believe to have fixed it. I noticed that the first argument passed into CreateDefaultAssociatedGroups has to be a site owner present in the user information list for that site. Retrieved one via Get-PnpUser, stored the result in an object and passed it as the first argument. Note however that if the groups are already present no duplicates will be created.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.