I've used the PnP Site provisioning method to create a sub web with unique permissions. However, when applying an XML site template with the default visitors, members, and owners security group defined in the template sections, the groups are not assigned. So what's the best method using PnP PowerShell to create sub webs with unique permissions and standard groups?

Here's my PowerShell:

#Connect to Site Collection
$me = Get-Credential
Connect-PNPOnline -url  https://mytenet.sharepoint.com/sites/MySiteCollection -credential $me

#Create sub web with unique permissions
New-PnPWeb -Title "ProjectB Web" -Url ProjectB -Description "Information about ProjectB"  -BreakInheritance -InheritNavigation -Template "STS#0"

#Apply template with groups.  Templates applies OK, but groups are not defined  
Apply-PnPProvisioningTemplate -Path C:\ProjectSiteTemplate.xml -Web projectB

This is a standard behaviour whenever we create a new Subweb using CSOM which is what ultimately PnP PowerShell is based upon.

So , you need to create and associate the groups yourselves using the New-PnPGroup and then Set-PnPGroup to associate the groups.

Something as below:

#Connect to Site Collection
$me = Get-Credential
Connect-PnPOnline -Url  https://mytenet.sharepoint.com/sites/MySiteCollection -Credentials $me

#Create sub web with unique permissions
New-PnPWeb -Title "ProjectB Web" -Url ProjectB -Description "Information about ProjectB"  -BreakInheritance -InheritNavigation -Template "STS#0"

Connect-PnPOnline -Url "https://mytenet.sharepoint.com/sites/MySiteCollection/ProjectB" -Credentials $me
$owner = (Get-PnPContext).Credentials.UserName

#Create default groups for the new web
#here, by default owner will be the person provisioning the groups
$ownerGroup = New-PnPGroup -Title "ProjectB Web Owners" -Owner $owner
Set-PnPGroup -Identity $ownerGroup -SetAssociatedGroup Owners

$memberGroup = New-PnPGroup -Title "ProjectB Web Members"  -Owner $owner
Set-PnPGroup -Identity $memberGroup -SetAssociatedGroup Members

$visitorGroup = New-PnPGroup -Title "ProjectB Web Visitors" -Owner $owner
Set-PnPGroup -Identity $visitorGroup -SetAssociatedGroup Visitors

#Apply template with groups.  Templates applies OK, but groups are not defined  
Apply-PnPProvisioningTemplate -Path C:\ProjectSiteTemplate.xml -Web projectB

Reference - Set PnPGroup

New PnPGroup

  • @DarellJohnson - if it worked for you, could you please accept it as answer ? Its just ticking the checkmark besides the answer. Cheers :) – Gautam Sheth Apr 12 at 12:45

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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