July 08, 2014

Office 365 Management - Part II - Delete All SubSites (Powershell)

688 Views

Overview

This is Part II of the blog sequence that deals with small utilities for management of Office 365 sites. Part 1 of this series demonstrate use of C# CSOM to delete sub sites. The current article presents an alternate option of using powershell to delete sub sites of an Office 365 site.

SharePoint Online powershell commands are very limited as of yet. That leaves us to use CSOM using powershell to perform necessary management activities for Office 365. Important point to note here is that you need to make use of few SharePoint Client side DLLs to perform the activities from powershell. The below script is specifically designed to recursively delete all sub sites under a given site on Office 365 tenant. However the script can easily be updated to perform other activities with all sub sites as well. I will follow up this article with some other utilities in near future.

Solution

Use SharePoint Online Management Shell for executing the script. If you do not have the shell already you can download it from here http://www.microsoft.com/en-in/download/details.aspx?id=35588

Build the credential that you would use to connect to SharePoint Online. You must use the tenant admin credential for this. The tenant admin credential looks like

adm.<userid>@<your tenant>.onmicrosoft.com

$credentials =  Get-Credential

# This would prompt for admin userid and password. Fill in those details.

# Register SharePoint Client DLLs. If you have Visual Studio 2012 or 2013 installed you might look up for these DLLs by searching in your system drive for “microsoft.sharepoint.Client*.dll”

# Alter the below DLL paths for your computer

Add-Type -Path "C:<ClientDLLS>Microsoft.SharePoint.Client.dll"

Add-Type -Path "C:<ClientDLLS>Microsoft.SharePoint.Client.Runtime.dll"

Add-Type -Path "C:<ClientDLLS>Microsoft.SharePoint.Client.Taxonomy.dll"

# connect/authenticate to SharePoint Online and get ClientContext object..

$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)

$clientContext.Credentials = $credentials

if (!$clientContext.ServerObjectIsNull.Value)

{

Write-Host "Connected to Office 365 site: '$Url'" -ForegroundColor Green

}

# Load the root site from the context

$rootWeb = $clientContext.Web

$clientContext.Load($rootWeb)

$clientContext.ExecuteQuery()

# Create a recursive delete function

function deleteWeb($web)

{

$clientContext.Load($web)

$clientContext.ExecuteQuery()

Write-Host "Web URL is" $web.Url

$subwebs = $web.Webs

$clientContext.Load($subwebs)

$clientContext.ExecuteQuery()

Write-Host "Child count: " $subwebs.Count

if ($web.Webs.Count -eq 0)

{

if ($web -ne $rootweb)

{

Write-Host "Deleting Site : " $web.Url

$web.DeleteObject()

$clientContext.ExecuteQuery()

}

}

else

{

foreach ($subweb in $subwebs)

{

Write-Host "SubWeb URL is" $subweb.Url

deleteWeb($subweb)

}

Write-Host "Deleting Site: " $web.Url

deleteWeb($web)

}

}

# Call the function

deleteWeb($rootWeb)

Leave a Reply

Your email address will not be published. Required fields are marked *