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
HTML code for above UI
- <table style="width:100%">
- <thead></thead>
- <tbody>
- <tr><td style="width:10%">Site Name:</td><td style="width:50%"><input ng-model="SiteName" type="text"/></td></tr>
- <tr><td></td><td><input type="button" ng-click="DeleteSubSite()" value="Delete Subsite"/><input type="reset" value="Cancel"/></td></tr>
- </tbody>
- </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
- $scope.DeleteSubSite = function(){
- if (confirm("Are you sure to delete SubSite")) {
- var subsite = $scope.SiteName;
- $.ajax({
- url: _spPageContextInfo.webAbsoluteUrl+"/"+subsite + "/_api/web",
- type: "POST",
- async: false,
- headers: {
- "accept": "application/json;odata=verbose",
- "content-type": "application/json;odata=verbose",
- "X-RequestDigest": $('#__REQUESTDIGEST').val(),
- "X-HTTP-Method": "DELETE"
- },
- success: function(data){
- alert(subsite +' has been deleted');
- },
- error: function(data){
- alert('Oops!! Its already deleted');
- }
- });
- }
- }
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
So mention the subsite name in the input of webpage as below,
Now click on the “Delete Subsite” button, then it will ask for conformation as below,
Please click on OK then the site will get deleted
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.