SharePoint Rest API: Create or Delete folder from a document library

This Rest API SharePoint tutorial explains, how to create a folder inside the document library using rest api in SharePoint Online/2013/2016. We will also discuss how to delete the folder from the document library in SharePoint 2013/2016/Online using rest api.

If you are new to rest api, then check out SharePoint rest api tutorial and examples.

SharePoint rest api create folder inside document library

Below is the rest api code to create a folder inside a document library in SharePoint. The code we can insert inside a script editor web part or content editor web part in a SharePoint web part page.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(function(){
$("#btnClick").click(function(){
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/folders";
$.ajax({
url: requestUri,
type: "POST",
data:JSON.stringify({'__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': 'Shared Documents/Folder1'}),
headers: {
"accept":"application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest":$("#_REQUESTDIGEST").val()
},
success: onSuccess,
error: onError
});

function onSuccess(data) {
alert('Folder created successfully');
}

function onError(error) {
alert(JSON.stringify(error));
}

});

});

</script>

<input type="button" id="btnClick" value="Create Folder Inside Document Library"/>

The above rest api code will create a folder inside Shared Documents document library in SharePoint.

Delete folder from Document library using Rest API SharePoint 2013/2016/Online

Below is the rest api code to delete a folder from a document library in SharePoint. The code we can insert inside a script editor web part or content editor web part in a SharePoint web part page.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<script>
$(function(){
$("#btnClick").click(function(){
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFolderByServerRelativeUrl('Shared Documents/Folder1')";
$.ajax({
url: requestUri,
type: "POST",
headers: {
"X-HTTP-Method":"DELETE"
},
success: onSuccess,
error: onError
});

function onSuccess(data) {
alert('Folder deleted successfully');
}

function onError(error) {
alert(JSON.stringify(error));
}
});

});

</script>

<input type="button" id="btnClick" value="Delete Folder From Document Library"/>

The above rest api code will delete the folder name as Folder1 from Shared Documents document library in SharePoint.

You may like following SharePoint rest api tutorials:

Hope this SharePoint rest API tutorial helps to know how to create a folder in the sharepoint document library using rest api. As well as we saw how to delete a folder from document library using rest api in SharePoint online or SharePoint 2013/2016.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

  • Ravi says:

    Not Run Your code

  • >