0

As the question basically says, is there a way to stop permissions inheritance after create a new website using powershell? Thanks a lot!

1

Since SharePoint 2013/Online supports a several sets of APIs, it depends which API you prefer:

  • CSOM
  • REST
  • SharePoint Web Sevices

How to break permissions inheritance for web site using CSOM

Prerequisite: download and install SharePoint Online Client Components SDK

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")


Function Get-ClientContext([string]$Url,[string]$UserName,[string]$Password)
{
    $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
    $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
    return $context
}



Function Web-BreakInheritance([Microsoft.SharePoint.Client.Web]$Web)
{
     $Web.BreakRoleInheritance($true, $false)
     $Web.Context.ExecuteQuery()
}

Usage

How to break permissions inheritance for Blogs site:

$UserName = "jdoe@contoso.onmicrosoft.com"
$Password = Read-Host -Prompt "Enter the password"    
$Url = "https://contoso.sharepoint.com/blog"

$context = Get-ClientContext -Url $Url -UserName $UserName -Password $Password
Web-BreakInheritance -Web $context.Web
$context.Dispose()
0

There is a property you can pass called UseSamePermissionsAsParentSite

$newWeb = New-Object Microsoft.SharePoint.Client.WebCreationInformation
$newWeb.Url = 'someURL'
$newWeb.UseSamePermissionsAsParentSite = $false
$newWeb.Title = 'Some Web'
$newWeb.WebTemplate = "STS#0"
$spWeb = $ctx.Web.Webs.Add($newWeb)
$ctx.Load($spWeb)
$ctx.ExecuteQuery()
0

Their is no easy way to break the inheritance in SPO as limited number of powershell cmdlt avaiable. Check this one : http://sharepointweblog.blogspot.com/2015/03/office-365-create-site-using-custom.html

But their is one custom script written for lists, You have to tweak this solution and run it after new site created.

Powershell script to break inherited permissions for all SharePoint Online lists in a site collection. Before you run it, you have to modify one last line of the script!https://gallery.technet.microsoft.com/office/Break-inheritance-for-all-ded251fa

function Set-SPOListBreakRoleInheritance
{

   param (
        [Parameter(Mandatory=$true,Position=1)]
        [string]$Username,
        [Parameter(Mandatory=$true,Position=2)]
        [string]$Url,
        [Parameter(Mandatory=$true,Position=3)]
        [string]$AdminPassword,
        [Parameter(Mandatory=$false,Position=4)]
        [bool]$IncludeSubsites=$false
        )



   $password = ConvertTo-SecureString -string $AdminPassword -AsPlainText -Force
  $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
  $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $password)
  $ctx.Load($ctx.Web.Lists)
  $ctx.Load($ctx.Web)
  $ctx.Load($ctx.Web.Webs)
  $ctx.ExecuteQuery()
  Write-Host 
  Write-Host $ctx.Url -BackgroundColor White -ForegroundColor DarkGreen
  foreach( $ll in $ctx.Web.Lists)
  {

        $ll.BreakRoleInheritance($true, $false)
    $ll.Update()

        try
        {
        $ctx.ExecuteQuery()
        Write-Host "Removed inherited permissions for " $ll.Title
        }
        catch
        {
        Write-Host "Failed to remove permissions for " $ll.Title
        }



        }









  if($ctx.Web.Webs.Count -gt 0 -and $IncludeSubsites)
  {
    for($i=0; $i -lt $ctx.Web.Webs.Count ; $i++)
    {
        Set-SPOListBreakRoleInheritance -Url ($ctx.Web.Webs[$i].Url) -Username $Username -AdminPassword $AdminPassword -IncludeSubsites $IncludeSubsites
    }

  }


  }


# Paths to SDK. Please verify location on your computer.
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" 



Set-SPOListBreakRoleInheritance -Username "trial@trialtrial123.onmicrosoft.com" -Url "https://trialtrial123.sharepoint.com" -AdminPassword "Pass" -IncludeSubsites $false 
3
  • This will work for on prem but the question was for SP Online (I think) – Bunzab May 6 '15 at 17:36
  • this is for SPO. – Waqas Sarwar MVP May 6 '15 at 17:41
  • 1
    It is now that you have changed it yes... – Bunzab May 6 '15 at 17:50

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.