How to Create a Folder in a SharePoint List using REST API in SharePoint 2013/2016/Office 365?

Hello SharePointers,

Below are the piece of REST API code to create a folder in a SharePoint list using REST API in  SharePoint 2013/2016/Office 365?

<script type=”text/javascript”>
function getFormDigest() {
return $.ajax({
url: _spPageContextInfo.webAbsoluteUrl + “/_api/contextinfo”,
method: “POST”,
headers: { “Accept”: “application/json; odata=verbose” }
});
}

function PreSaveItem() {
var folderName=$(“input[title=’Title Required Field’]”).val(); //get new list item title
if(folderName!=””){
createfolder(folderName).done(function (data) {
console.log(‘Folder creatted succesfully’);
}).fail(function (error) {
console.log(JSON.stringify(error));
});
}
return true;
}
function createfolder(folderName){
var documentLibraryName = “EmployeeFolder”;
var requestUri = _spPageContextInfo.webAbsoluteUrl + “/_api/web/folders”;
return getFormDigest().then(function (data) {
return $.ajax({
url: requestUri,
type: “POST”,
contentType: “application/json;odata=verbose”,
data:JSON.stringify({‘__metadata’: { ‘type’: ‘SP.Folder’ }, ‘ServerRelativeUrl’: documentLibraryName+’/’+folderName}),
headers: {
“accept”:”application/json;odata=verbose”,
“X-RequestDigest”:data.d.GetContextWebInformation.FormDigestValue
}
});
});
}
</script>

Happy SharePointing ­čÖé

Leave a Reply

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