From one of our customers I was asked, if it is possible to disable the creation of subsites in OneDrive. Hm. We should not change any permissions of the user in his OneDrive site. So, by his permissions, he will always be able to create new sites. But we can remove all the web templates from the site collection. He still is able to open the “Create new site” dialog, but he is not able to choose a template for his site.

All could be done by some PowerShell. I have split the scripts into three parts. The first one is DisableSiteCreation.ps1. This will do the changes for a given url.

param (
    [Parameter(Mandatory=$true)][string] $Url
)

Write-Host "$Url.Url..."

$web = Get-SPWeb $Url

if ($web.Provisioned -eq $false)
{
    Write-Host -ForegroundColor Yellow "The web at $Url is not provisioned. Skip the web"
}
else
{
    $col = New-Object System.Collections.ObjectModel.Collection[Microsoft.SharePoint.SPWebTemplate]

    $cultures = $web.SupportedUICultures
    foreach ($culture in $cultures)
    {
        $lcid = $culture.LCID
        # Write-Host $culture.LCID

        $w = Get-SPWeb $Url
        $w.SetAvailableWebTemplates($col, $lcid)
        $w.Update()

        Write-Host "Removed web templates in $Url for LCID $lcid"
    }
}

The second script is RunOnSiteCollections.ps1 that is used to call the first script. The path to the first script (“DisableSiteCreation.ps1”) is passed as a parameter to this script.

param (
    [Parameter(Mandatory=$true)][string] $WebApp,
    [Parameter(Mandatory=$true)][string] $Script,
    [string] $FilterTemplate,
    [string] $FilterConfiguration
)

$wa = Get-SPWebApplication $WebApp -ErrorAction SilentlyContinue

if ($wa -eq $null)
{
    Write-Host -ForegroundColor Red "There is no web application in url $WebApp"
}
else
{
    foreach ($site in $wa.Sites)
    {
        $web = $site.RootWeb

        $runOnWeb = $true

        if (($FilterTemplate -ne "") -and ($FilterConfiguration -ne ""))
        {
            if (($FilterTemplate.ToUpper() -ne $web.WebTemplate.ToUpper()) -or ($FilterConfiguration -ne $web.Configuration))
            {
                $runOnWeb = $false
            }
        }
        
        if ($runOnWeb -eq $true)
        {
            & $Script $web.Url
        }
    }
}

The last one DisableSiteCreationInMySites.ps1 is just a wrapper to call the functionality. This last script will get the url of the MySite host and will remove all the web templates from the site collections in this web application. Run this one to disable the site creation in OneDrive sites.

param (
    [Parameter(Mandatory=$true)][string] $WebApp
)

. .\RunOnSiteCollections.ps1 -WebApp $WebApp -Script .\DisableSiteCreation.ps1 -FilterTemplate SPSPERS -FilterConfiguration 10

To revert these changes, the following scripts could be used, whereby the script EnableSiteCreationInMySites.ps1 should be called.

The counterpart to the DisableSiteCreation.ps1 is EnableSiteCreation.ps1:

param (
    [Parameter(Mandatory=$true)][string] $Url
)

Write-Host "$Url.Url..."

$web = Get-SPWeb $Url

if ($web.Provisioned -eq $false)
{
    Write-Host -ForegroundColor Yellow "The web at $Url is not provisioned. Skip the web"
}
else
{
    $web.AllowAllWebTemplates()
    $web.Update()

    Write-Host "All web templates are allowed in $Url"
}

The counterpart to DisableSiteCreationInMySites.ps1 is EnableSiteCreationInMySiteHost.ps1:

param (
    [Parameter(Mandatory=$true)][string] $WebApp
)

. .\RunOnSiteCollections.ps1 -WebApp $WebApp -Script .\EnableSiteCreation.ps1 -FilterTemplate SPSPERS -FilterConfiguration 10

 

Advertisements