Get All Deleted SharePoint Sites From Tenant Using Power Automate

Introduction

Developers do automation to make sites and teams implement processes in organizations.

Recently, I was part of one of the automation projects and wanted to check requested site name exists or not in the Tenant level Site Recycle bin.

Yes, it is possible to check using PowerShell script in logic apps but in my case, the client wouldn’t want to use logic apps because to reduce cost. I implemented power automation by using a standard SharePoint connector to check deleted sites.

Learning Objectives

  1. Create Manual Trigger Power Automate
  2. Generate Request Digest
  3. Query to get deleted sites
  4. Output

Prerequisite

  • M365 environment
  • Power automate access requires a license
  • SharePoint Admin Access

Create Manual Trigger Power Automate

Click on Create new Power Automate and Select Manual trigger option.

Create a parameter called “SiteURL”.

Get All Deleted SharePoint Sites From Tenant Using Power Automate

Initialize variable with name “RequestDigest”.

Initialize the variable with the name “GetDeletedSitesProcessQuery” and add the below value to it.

<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName="PowerAutomate" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009"><Actions><ObjectPath Id="2" ObjectPathId="1" /><ObjectPath Id="4" ObjectPathId="3" /><Query Id="5" ObjectPathId="3"><Query SelectAllProperties="false"><Properties /></Query><ChildItemQuery SelectAllProperties="true"><Properties><Property Name="Url" ScalarProperty="true" /><Property Name="SiteId" ScalarProperty="true" /><Property Name="DaysRemaining" ScalarProperty="true" /><Property Name="Status" ScalarProperty="true" /></Properties></ChildItemQuery></Query></Actions><ObjectPaths><Constructor Id="1" TypeId="{268004ae-ef6b-4e9b-8425-127220d84719}" /><Method Id="3" ParentId="1" Name="GetDeletedSitePropertiesFromSharePoint"><Parameters><Parameter Type="String">0</Parameter></Parameters></Method></ObjectPaths></Request>

Initialize variable called "DeletedSites" with type array.

Generate Request Digest Value

Request Digest Value: When executing non-GET REST requests to the SharePoint REST API, you must include a valid request digest to your request. The digest proves the validity of your request to SharePoint. Because this token is valid only for a limited period, you must ensure the token is valid before adding it to your request else the request fails.

Click on Add new action and select “Send an HTTP request to SharePoint”.

Set Below Parameter to it.

Site Address:https://companyName-admin.sharepoint.com
Method: Post
Uri: /_api/contextinfo

Headers

{
  "accept": "application/json;odata=nometadata"
}

Set Request Digest Value from the output of the previous action.

outputs('Create_Request_Digest_Value')?['body']?['FormDigestValue']

Query to get deleted sites

Click on add new action and select “Send an HTTP request to SharePoint”.

Site Address: https://companyName-admin.sharepoint.com

Method: Post

Uri: /_vti_bin/client.svc/ProcessQuery

Headers

{
  "X-RequestDigest": @{variables('RequestDigest')}
}

Body : @{variables('GetDeletedSitesProcessQuery')}

Filter deleted sites

Set variable “DeletedSites” from the output from previous steps.

Output

[
  {
    "_ObjectType_": "Microsoft.Online.SharePoint.TenantAdministration.DeletedSiteProperties",
    "_ObjectIdentity_": "aa9af29f-e0c0-0000-b847-856ad1deff2f|908bed80-a04a-4433-b4a0-883d9847d110:3d24e369-128a-419f-a457-659738339df3\nDeletedSiteProperties\nhttps%3a%2f%2fdev1802.sharepoint.com%2fsites%2fsdds",
    "DaysRemaining": 71,
    "DeletionTime": "/Date(2021,8,3,6,15,27,647)/",
    "SiteId": "/Guid(a34cee60-c0d7-40bb-ad40-e0614a9f428e)/",
    "Status": "Recycled",
    "StorageMaximumLevel": 26214400,
    "Url": "https://dev1802.sharepoint.com/sites/sdds",
    "UserCodeMaximumLevel": 300
  }
]