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()

3 Answers 3

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 = "[email protected]"  
$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  
} 
1
  • Thanks. Almost there but no success yet. I believe it must have something to do with the arguments passed to the CreateDefaultAssociatedGroups method... Commented Nov 28, 2018 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.

0

Get the $Connection by running Connect-PnPOnline and run this (assuming you want use Visitors as default):

$Visitors  = Get-PnPGroup -AssociatedVisitorGroup -Connection $Connection  
Set-PnPGroup -Identity $Visitors -SetAssociatedGroup Members -Connection $Connection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.