3

I want to hijack the call to CoreInvoke on Adding a new document for a given SharePoint online document library so I can prompt the user for some metadata and populate the Word document with it automatically.

What's the best way to do this?

The documents library is a custom content type with a template. I have form fields in the MS Word document which should be automatically set to a handful of library columns. This is SharePoint Online.

I've tried CoreInvoke but can only get it to open a new document using the template. I've tried using the FileReader API (https://msdn.microsoft.com/en-us/library/office/dn769086.aspx) but that's going to require the user to select the template manually each time.

Any help is appreciated!

1 Answer 1

0

The best thing is to not try to alter default behavior of SharePoint. It will break the user experience (different functionality on same type of action).

I would create a JavaScript function for this using the SharePoint JavaScript API or REST API. You can then add a "script editor" web part on the page with a "Create new document" button in html. The "click" on the button show your custom input dialog and creates the new document with the correct metadata.

see here for some tips on the JavaScript part: https://msdn.microsoft.com/en-us/library/office/jj163201.aspx

This is just a sample

  • Create a javascript file newDocumentButton.js
  • Create a css file newDocumentButton.css
  • Add the files to a folder in "master page gallery" or "style library". Best to create your own folder for the "solution".
  • Add a script web part to the page where you would like to add your "new document button".
  • Add this snippet to the script web part:

    <link rel="stylesheet" type="text/css" href="/_catalogs/masterpage/demo/Styles/NewDocumentButton.css"/>
    <script src="https://code.jquery.com/jquery-2.2.4.min.js" ></script>
    <script type="text/javascript" src="/_catalogs/masterpage/demo/Scripts/NewDocumentButton.js"></script>
    <div id="btn-newDocument">
    </div>
    
  • Add some code in the JavaScript file to create the dialog and for getting user input:

    $(document).ready(function () { 
        var buttonHtml = "<input id=\"newDocumentButton\" type=\"button\" onclick=\"javascript:SP.UI.ModalDialog.ShowPopupDialog('" + prefix + "/_layouts/15/CreatePublishingPageDialog.aspx?IsDlg=1')\" value=\"New Document\">";      
    $('#btn-newDocument').append(buttonHtml);
    });
    
  • Make sure you got a reference to JQuery or other js library.

  • Add functionality to the JavaScript file for creating dialog, add document etc.

Happy coding :-)

1
  • Please do not bind jQuery for such small solutions. In order to execute functions at page loading there is a SharePoint built in function _spBodyOnLoadFunctionNames.push(function HGV_Custom(){});
    – Emaborsa
    Commented Jul 2, 2018 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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