SPO JS Create new file from template

Connor Johnston 31 Reputation points
2020-10-16T10:05:32.147+00:00

Hi,

I have a custom ASPX page in SPO that creates/uploads documents into a library, and in said library there are multiple document types with templates set. Ideally I would like to replicate the behaviour of the New > Content Type name UI in the document library shown below.

32885-screenshot-2020-10-16-110058.png

So far I have tried

  • Creating file with empty byte array via CSOM & REST, then setting content type. Template doesn't load.
  • Creating file with SP JS - CoreInvoke('createNewDocumentWithRedirect2' ...) Appears to do nothing, so guessing this method is 2013 JS method
  • Copying template .dotx to desired destination and renaming to .docx via REST, file is empty

Any info on how SP JS creates files from templates would be much appreciated, or if CoreInvoke('createNewDocumentWithRedirect2' is still valid, what the correct properties are.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,809 questions
0 comments No comments
{count} votes

Accepted answer
  1. Baker Kong-MSFT 3,801 Reputation points
    2020-10-19T10:31:04.507+00:00

    Hi @Connor Johnston ,

    Please take a reference of below code, it fetches the content type template and uploads it through rest API.

    I have tested it in a Content editor web part and it works well in my SPO environment:

    33373-test.gif

    The reason why the file is empty is that the file format gets broken during the upload.

    Another possible method:

    <a href="ms-word:nft|u|https://abc.sharepoint.com/sites/testprivate/mytestdoclib/Forms/CTName/CAT.docx|s|https://abc.sharepoint.com/sites/testprivate/mytestdoclib"><h2>New Document</h2></a>

    Best Regards,
    Baker Kong


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.