We need to create a text file and upload it to a document library using REST API. The contents of text file will be created dynamically by reading information from multiple lists. The operation needs to be performed using REST API and file is not to be saved in user's machine. Is this possible?
1 Answer
Yes, its possible.
Try and modify the below code. Fill the content with data from your lists:
var content = "Hello, this text is inside the file created with REST API";
var digest = $("#__REQUESTDIGEST").val();
var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getfolderbyserverrelativeurl('/sites/test/Shared Documents')/Files/add(overwrite=true, url='test.txt')";
jQuery.ajax({
url: url,
type: "POST",
data: content,
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": digest
},
success: function(data){
console.log(data);
},
error: function(data){
console.log(data);
}
});
-
Didn't realize it was that simple...! Thanks.Naveen– Naveen2018-01-15 10:23:09 +00:00Commented Jan 15, 2018 at 10:23