Create SharePoint List/Library using REST API
Use the below code to create a SharePoint document library using REST API. You can change the 'BaseTemplate' ID for creating list/library based on your requirment.
function createNewLibraryUsingRest()
{
var newListName = "Your New Library Name";
var deferred = $.Deferred();
var serverURL = _spPageContextInfo.webAbsoluteUrl;
var listURL = String.format("{0}/_api/Web/Lists/",serverURL);
$.ajax({
url: listURL,
method: "POST",
data: JSON.stringify({ '__metadata': { 'type': 'SP.List' },
'AllowContentTypes': true,
'BaseTemplate': 101, //101 is for document library
'ContentTypesEnabled': true,
'Description': 'My list description',
'OnQuickLaunch': true,
'Title': newListName
}),
headers: { "content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function(data){
alert('New Library Created!');
deferred.resolve();
},
error: function (err) {
alert(err.responseText);
deferred.reject();
}
});
return deferred.promise();
}
Comments
Post a Comment