3.19.2014

SharePoint 2010 Force Modal Dialog

The Problem:

You've added a Custom Action to your SharePoint 2010 list to redirect users to a specific edit form.  However, when they click on it, it takes them to a completely separate page, away from the list.  Lets take a look at how we can force SharePoint to open these links in the Modal Dialog similar to how it handles the default edit/create/display forms.

The Solution:

So, in SharePoint Designer, under Lists and Libraries, you have selected the list you're working on.  In the bottom right-hand corner of the List page, you should see a Custom Actions box.  Create your Custom Action if you haven't already.


Now that you've got your Custom Action started, we are going to work off of the default settings in the "Navigate to Form" field.  We will use a simple JavaScript function call to SP.UI.ModalDialog.showModalDialog() which is a built-in JS function included with SharePoint 2010.

Here is an example of the call:

javascript:SP.UI.ModalDialog.showModalDialog({
url: "/sites/<SiteName>/Lists/<ListName>/EditForm.aspx?ID={ItemId}",
title: "Custom Edit Form/Destination",
dialogReturnValueCallback:  function(dialogResult){
SP.UI.ModalDialog.RefreshPage(dialogResult);
location.reload();
 }
});
return false;
After editing the URL & Title, this function call can be placed right into the "Navigate to URL" textbox back on the SharePoint Designer Custom Action screen.

You might notice there is a line called dialogReturnValueCallback.  Here, we specify the behavior to send back to the original page that called the Modal (our list).  For this example, we refresh the original list to reflect any changes that may have been made on the Modal Dialog.



Copy the JavaScript (inline) right into the Custom Action "Navigate to URL" box, click "OK" and test out your custom action!

** UPDATE (6/24/15) - I noticed when using the above code snippet, that SharePoint Designer won't let you copy and paste the code as it is above.  In order to use the code sample above, you must remove all carriage returns and paste the code in as a single line:

javascript:SP.UI.ModalDialog.showModalDialog({ url: "/sites/<SiteName>/Lists/<ListName>/EditForm.aspx?ID={ItemId}", title: "Custom Edit Form/Destination", dialogReturnValueCallback:  function(dialogResult){ SP.UI.ModalDialog.RefreshPage(dialogResult); location.reload(); }});return false;

5 comments:

  1. Every time I add the link it reverts back to navigate to form upon save. Only does this on a link to a form.

    ReplyDelete
    Replies
    1. When you navigate back to the Custom Action form in SharePoint Designer, it will default back to Navigate to form. However, the Custom JS that was saved to the 'Navigate to URL' box WILL be saved. In order to edit it, you will have to use an external text editor and copy&paste each time.

      Delete
  2. First of all you should replace any space in your url with %20. For example if you have a list named "Sample List" the JavaScript becomes

    javascript:SP.UI.ModalDialog.showModalDialog({
    url: "/sites/project/Lists/Sample%20List/EditForm.aspx?ID={ItemId}",
    title: "Custom Edit Form/Destination",
    dialogReturnValueCallback: function(dialogResult){
    SP.UI.ModalDialog.RefreshPage(dialogResult);
    location.reload();
    return false;
    }
    });


    Also note that I moved "return false;" statement after "location.reload();" because return statements can only be inside functions.

    ReplyDelete
    Replies
    1. Hi thanks for your response.

      1. The spaces in the URL will be automatically replaced with '%20' by the browser. Feel free to do it yourself if you'd like, but it works either way.

      2. The "return false;" has nothing to do with the function call. It is there because this code is being attached to an event handler (onmenuclick) on the list view page. The "return false" cancels the default behavior of the browser (in this case following the link). Without it, the browser will redirect to the Custom Form on a separate page before our custom JavaScript has a chance to fire and bring up the Modal.

      Delete
  3. Hi,

    When I attempt to save my custom action with this script, I get the following error. Any advice?

    Could not save the list changes to the server. 'sites' is an unexpected token. The expected token is '>'. Line 1, position 568.

    Thanks!

    ReplyDelete