0

I'm trying to add some buttons to a new modern page (SP 365) that I have previously done in older versions of SharePoint (classic pages), the buttons are to create a new document (Word, Excel) from a template

This is my basic code trying to get it working:

<button id="myButton">Click me</button>
<script>
document.getElementById("myButton").addEventListener("click", newDoc);

function newDoc() {
  CoreInvoke('createNewDocumentWithRedirect2', event, 'https://mysite.sharepoint.com/Documents/Forms/template.dotx', '', 'SharePoint.OpenDocuments', false, 0, true, 0);
}
</script>

At first when clicking on the button I was getting an error that CoreInvoke was not defined, so I added the script imports:

<SharePoint:ScriptLink name="sp.js" runat="server" OnDemand="true" LoadAfterUI="true" Localizable="false" />
<SharePoint:ScriptLink name="sp.core.js" runat="server" OnDemand="true" LoadAfterUI="true" Localizable="false" />
<SharePoint:ScriptLink name="sp.runtime.j" runat="server" OnDemand="true" LoadAfterUI="true" Localizable="false" />
<SharePoint:ScriptLink name="SP.UI.Controls.js" runat="server" OnDemand="true" LoadAfterUI="true" Localizable="false" />
<SharePoint:ScriptLink name="clienttemplates.js" runat="server" OnDemand="true" LoadAfterUI="true" Localizable="false" />
<SharePoint:ScriptLink name="clientforms.js" runat="server" OnDemand="true" LoadAfterUI="true" Localizable="false" />
<SharePoint:ScriptLink name="autofill.js" runat="server" OnDemand="true" LoadAfterUI="true" Localizable="false" />
<SharePoint:ScriptLink name="sp.RequestExecutor.js" runat="server" OnDemand="true" LoadAfterUI="true" Localizable="false" />

Now when clicking on the button nothing happens and no errors in the console, and actually adding these seems to break any scripts from executing as not even a simple alert() works with these included

Many tutorials are outdated now so I'm not sure if this still works in todays SP Online with modern pages using either createNewDocumentWithRedirect2 or with createNewDocumentWithProgId

To add the script to the page, I installed the react-script-editor web part

Edit - Custom scripting is enabled, if I do a very simple test such as the following, it works:

<button id="myButton">Click me</button>
    <script>
    document.getElementById("myButton").addEventListener("click", function() { alert('works!');});
</script>

My ultimate question is not about getting scripts to work, it's about ability to use createNewDocumentWithRedirect2 on a modern page

4
  • Add script using <script> tag
    – P S
    Aug 8 '19 at 10:25
  • Doesn't answer my question, and as you can see the script tags are there
    – MikeM
    Aug 8 '19 at 13:38
  • Hi Mike, what i wanted to say was, instead of <SharePoint:scriptlink> use <script> tag and give the path of script file in source parameter. But I can see you found the answer by other way.
    – P S
    Aug 9 '19 at 8:33
  • Yep, I had done that as well with no luck
    – MikeM
    Aug 9 '19 at 12:13
0

The modern site is not supporting for the custom script by default. So you can try by enabling the Scripting Capabilities with PowerShell.

  1. Download & Install SharePoint Online Management Shell
  2. Open SharePoint Online Management Shell Connect to your SharePoint Admin Portal
  3. Execute the PowerShell command as mentioned below:

    Set-SPOsite https://your-domain.sharepoint.com/site-path -DenyAddAndCustomizePages 0

That's it.

1
  • Custom scripting is already enabled as evidenced by my test script actually working, though this doesn't answer my ultimate question of how to or can I use CoreInvoke with createNewDocumentWithRedirect2 on a modern page? - editing my original post to state that custom scripting is enabled
    – MikeM
    Aug 8 '19 at 13:32
0

So it looks like doing things with the javascript route isn't necessary anymore, looking into the network calls that are made when clicking on New in the document library then selecting a template reveals the answer

The link it does a GET on is formatted with the Office URI scheme:

ms-word:nft|u|<template link>|s|<doc library save folder link>

I'm not sure if there is a script constructing this link in the background, but doesn't appear needed

The save folder link is not necessary

So formatting a simple link with the uri scheme and the nft command does the job!

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.