3

I have a document library with a content type called 'Letter' attached to it. The 'Letter' content type has a MS Word document template attached to it.

I would like to know how, using REST, to create a new instance of this content type in my document library, so I end up with a copy of the content type's MS word template document in the document library.

Thanks for your help!

  • What have you tried sofar and where do you get stuck? This is a place where we share code – Danny '365CSI' Engelman Apr 28 '16 at 15:44
1

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:

  1. 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)

  2. 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...

  • 2
    Thanks Gintas. For any one else reading this - It's worth noting that you can't copy templates from the document set /forms folder. You need to add the templates to a document Library and copy them from there. – M.Jones Apr 29 '16 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.