I've recently had the same need. Unfortunately, I think there's no way to create a new document using REST, because you actually have to UPLOAD something.
In my solution, I made a copy of the template and uploaded it to the library.
var templateString = 'yourSite/pathToTemplate/templateName.docx';
var newDocString = 'listInCurrentWeb/newDocumentsName.docx';
copyTo(templateString,newDocString)
.done(function(data){
console.log('success: ' + data);
})
.fail(function(error){
console.log('error: ' + JSON.stringify(error));
});
function copyTo(sourceFileUrl,targetFileUrl, success,error) {
var endpointUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getfilebyserverrelativeurl('" + sourceFileUrl + "')/copyto(strnewurl='" + targetFileUrl + "',boverwrite=false)";
return $.ajax({
url: endpointUrl,
method: "POST",
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
});
}
NOTES:
When using this method, make sure that newDocumentsName
is dynamic, because it has to be unique, unless you want to override the existing document with the same name (you can do this by setting boverwrite
to true
)
You can only use this method to copy items within current web.
I know it's not what you expected, but that's the only solution I came up with after a week of research...