Delete A SharePoint Subsite By Using Angular And Rest API

Introduction

In this article I am going to explain how to delete a subsite programmatically using Angular js and rest API call.

Over view

  • Create a web page to give the name of subsite which you want to delete
  • Write a script code for delete the subsite on click of button.

Procedure

Step1

Create a web page as below to give the subsite name to perform deletion

SharePoint Subsite
HTML code for above UI 

  1. <table style="width:100%">  
  2.       <thead></thead>  
  3.         <tbody>  
  4.         <tr><td style="width:10%">Site Name:</td><td style="width:50%"><input ng-model="SiteName" type="text"/></td></tr>  
  5.         <tr><td></td><td><input  type="button" ng-click="DeleteSubSite()" value="Delete Subsite"/><input type="reset" value="Cancel"/></td></tr>  
  6.         </tbody>  
  7.     </table>  

Step 2 - Code for DeleteSubsite button

  • get the input values of site name from web page to perform deletion
  • pass the name of subsite on URL as shown in below

  1. $scope.DeleteSubSite = function(){/* calling the function of delete subsite on button click*/  
  2.         if (confirm("Are you sure to delete SubSite")) {/*asking for confirmation*/  
  3.             var subsite = $scope.SiteName;/*Setting the subsite name to a variable*/  
  4.             $.ajax({      
  5.             url: _spPageContextInfo.webAbsoluteUrl+"/"+subsite + "/_api/web",    /*Passing the variable in the rest api URL to perform deletion on the particular Subsite*/  
  6.             type: "POST",    
  7.             async: false,    
  8.             headers: {    
  9.                  "accept""application/json;odata=verbose",    
  10.                 "content-type""application/json;odata=verbose",    
  11.                 "X-RequestDigest": $('#__REQUESTDIGEST').val(),      
  12.                 "X-HTTP-Method""DELETE"    
  13.             },     
  14.             success: function(data){    
  15.                  alert(subsite +' has been deleted');    
  16.             },      
  17.             error: function(data){     
  18.                 alert('Oops!! Its already deleted');     
  19.             }      
  20.         });   
  21.        }  
  22.     }   

Let’s see the result on screen as below,

I have a subsite named “MySampleSubsite” and type is “team site” that I going to delete

SharePoint Subsite
So mention the subsite name in the input of webpage as below,

SharePoint Subsite
Now click on the “Delete Subsite” button, then it will ask for conformation as below,

SharePoint Subsite

Please click on OK then the site will get deleted

SharePoint Subsite
Hence, we have deleted the subsite successfully using Angular js and REST api

You can go back to your subsite and refresh, it will show an error that page not found.

Conclusion

This article helps you to delete a subsite of a SharePoint programmatically using Angular js and REST api.

Up Next
    Ebook Download
    View all
    Learn
    View all