Finally got it to work using REST API. But it involves additional REST call.
Below is the code to Create a folder, from that code we will call the get Folder method to get the folder's ID:
function CreateFolder(){
jQuery.ajax({
"url" : _spPageContextInfo.siteAbsoluteUrl + "/_api/Web/Folders/add('/sites/testsite/Shared Documents/newfolderRest16')",
"type" : "POST",
"headers" : {
"accept" : "application/json; odata=verbose",
"content-type" : "application/json; odata=verbose",
"X-RequestDigest" : $("#__REQUESTDIGEST").val()
},
success: function(data){
console.log(data);
GetFolder(data);
},
error: function(data){
console.log(data);
}
});
}
Now, getFolder method will get the folder's ID which we will then pass to the UpdateFolder method to update the folder:
function GetFolder(data){
var url = _spPageContextInfo.siteAbsoluteUrl + "/_api/web/getfolderbyserverrelativeurl('/sites/testsite/Shared Documents/newfolderRest16')?$expand=ListItemAllFields";
$.getJSON(url).then(function(data){
console.log(data.ListItemAllFields.ID);
UpdateFolder(data.ListItemAllFields.ID);
});
}
function UpdateFolder(id){
$.ajax ({
url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('Documents')/items("+ id +")",
type: "POST",
data: JSON.stringify({
"__metadata":
{
type: "SP.Data.Shared_x0020_DocumentsItem"
},
"Department": "HR",
"Location": "India"
}),
headers:
{
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"IF-MATCH": "*",
"X-HTTP-Method": "MERGE"
},
success: function(data, status, xhr)
{
console.log("success");
},
error: function(xhr, status, error)
{
console.log(error);
}
});
}
here, in the update folder method, please ensure that you are using the correct ListItemEntityTypeFullName
or else it will throw bad request error.
To get the correct ListItemEntityTypeFullName
, make a REST call to the below endpoint in the browser.:
https://sitecollectionurl/_api/web/lists/getbytitle('Documents')?$select=ListItemEntityTypeFullName