SharePoint Modal - paste value back to parent when closing

1.2k views Asked by At

I've created a custom ribbon button where i load external content into a Modal Dialog. I want to insert some text on the editor and close the dialog. The function OnPopClosed is running when I click something on my dialog but I'm having an error: RTE is not defined, so I can't insert anything on the editor. Any ideas?

function init(){
    var options = SP.UI.$create_DialogOptions();
    options.title = "Sharepoint Plugin";
    options.width = 600;
    options.height = 400;
    options.url = '/_Layouts/Test/Test.aspx';
    options.dialogReturnValueCallback = OnPopClosed;
    SP.UI.ModalDialog.showModalDialog(options);
}
function OnPopClosed(test) {

    var range = RTE.Cursor.get_range();
    range.deleteContent();
    var selection = range.parentElement();
    if (!selection) {
        return;
    }

    range.insertNode(elem);
    RTE.Cursor.get_range().moveToNode("test");
    RTE.Cursor.update();

    SP.UI.ModalDialog.commonModalDialogClose(1, "test");
}
1

There are 1 answers

1
Vadim Gremyachev On BEST ANSWER

Place RTE actions in SP.SOD.executeOrDelayUntilScriptLoaded in order to execute the specified function if the file containing it is loaded (in your case sp.ui.rte.js):

ExecuteOrDelayUntilScriptLoaded(RTEActions, "sp.ui.rte.js");

Example:

function RTEActions()
{
    var range = RTE.Cursor.get_range();
    //Remaining code goes here...
    SP.UI.ModalDialog.commonModalDialogClose(1, "test");
}

then declare the dialog handler like this:

function OnPopClosed() {
   ExecuteOrDelayUntilScriptLoaded(RTEActions, "sp.ui.rte.js");
}