Stop Redirection When Form Is Opened In SP.UI.ModalDialog

I am opening a from using SP.UI.ModalDialog.showModalDialog method.

I need to link the item that is created to an item in another list when the form is saved.

I found the following code in other forum posts about setting the RedirectUrl to null, making the api call and than resetting the RedirectUrl. This isn't working for me when the form is opened in the dialog. How can I get this to work?

fd.spSaved(result => {
  var redirect = result.RedirectUrl;
  result.RedirectUrl = null;
  sp.web.lists.getByTitle("Other List").items
      .getById(ItemID)
      .update({
          LinkedField: result.Id
      })
      .then(function() {
          window.location.href = redirect;
      });
});

Hello @jpeszleny,

Welcome to the Plumsail Community!

I can think of two options:

  • Replace the default Save button with a custom button that first runs the code to update the list item and then saves the form in a dialog.
  • Open the dialog again once the new item is created and then update the list item.

Thanks! A custom button it is.

I added a custom button but the form still closes when I save the form. I need to save the form and get the item id. What is causing the form to close after saving?

fd.toolbar.buttons.unshift({
    class: "btn-primary",
    text: "Save",
    click: function () {
       fd.save();
    }
});

@jpeszleny,

fd.save() closes the dialog or redirects to a previous page.

If you need item ID, the only option for you is to open the dialog after the new item is saved. You can try using the dialogReturnValueCallback as described in this post:

Also, may I know why are you using dialog instead of the panel? Panels are more native to modern UI.

I made it work using the dialogReturnValueCallback and fd.spSavedevent.

I'm opening the form in a SP.UI.ModalDialog because it's coming from an on premises app deployed in a server side solution (wsp).