locked
How to close the sharepoint modal dialog and refresh the parent page using Server side API? RRS feed

  • Question

  • I am trying to find a way to close a modal page that has been opened with the click event of ribbon control. Once the modal dialog is opened, I'm showing 'OK' button for the user to confirm some action on the respective list item. If the user clicks on 'OK', I'm performing some action on the listitem using server side code. Here the problem for me is, once the action is completed on this listItem, I need to close the dialog and refresh the parent page (AllItem.aspx) using *sharepoint server side
    API*

    I'm aware that for closing the dialog we have JS code "SP.UI.ModalDialog.commonModalDialogClose". But this is not useful for me, because I should first run some action on the listitem and once that action is complete, then only I should close this dialog.
    Friday, February 19, 2010 12:15 PM

Answers

  • I spoke with the owner of the SharePoint Dialog Platform and he provided the following information:

    The solution is to write JS directly to the http Response object that will close the dialog. In order to have it refresh the page when the dialog is closed, you can pass in SP.UI.ModalDialog.RefreshPage as the dialogReturnValueCallback function.

    The server-side code should look like:
    Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
    Context.Response.Flush();
    Context.Response.End();

    Hope this helps

     


    Jonathan Kern :: UX Developer :: SharePoint Foundation
    Tuesday, February 23, 2010 11:03 PM

All replies

  • First option would be to use Client Side Object Model with JavaScript , to perform operations on the list item. But I am assuming you already rule this option out because of some custom server side logic which cannot be ported into client side. Another option is to use Ajax and call server side code via WCF , this is quite powerful concept but you need to do bit of more work to get it all work out for you. Dan post on "Service Oriented Ajax Dev on SharePoint 2010" , may get you started on this http://daniellarson.spaces.live.com/blog/cns!D3543C5837291E93!6887.entry 
    http://razi.spaces.live.com/
    Tuesday, February 23, 2010 4:43 PM
  • I spoke with the owner of the SharePoint Dialog Platform and he provided the following information:

    The solution is to write JS directly to the http Response object that will close the dialog. In order to have it refresh the page when the dialog is closed, you can pass in SP.UI.ModalDialog.RefreshPage as the dialogReturnValueCallback function.

    The server-side code should look like:
    Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
    Context.Response.Flush();
    Context.Response.End();

    Hope this helps

     


    Jonathan Kern :: UX Developer :: SharePoint Foundation
    Tuesday, February 23, 2010 11:03 PM
  • hello jkern,

    i'm also face same prob...

    can u show me how to do that??

    if posibble with some snippet...

    thanks in advance...

    Wednesday, March 24, 2010 2:33 PM
  • The code snippet is above. Just do your server-side logic and then run those lines of code at the end of your method to close the dialog.
    Jonathan Kern :: UX Developer :: SharePoint Foundation
    Wednesday, March 24, 2010 8:07 PM
  • I am having a similar issue. My issue is that I cannot get a click event to fire on the server side when the button is pressed in the modal window.

    I have a div on the page that I am popping up in the modal. It's a repeater which lists out several check boxes and radio buttons. The user can select multiple options and I have 2 buttons at the bottom, one to select everything, the other to cancel it all. It all pops up fine using the showmodaldialog(options) javascript method. When a user clicks on the button to select it doesn't hit the click method in the server side code. So I can't have it run the Response actions you have listed above. Is there a particular reason why it won't let me do this?

    I have also confirmed that if I don't have it open in the modal my code works. But for some reason when it's in the modal it doesn't find the click event code in the code behind.

    • Edited by KPaxton Thursday, July 1, 2010 8:16 PM adding more info.
    Thursday, July 1, 2010 8:15 PM
  • The code above provided by Jonathan works fine. I just tried it in a custom application page. Here's what I have on the front end as well as on the code behind.

    ASPX markup

    <asp:Button ID="Button2" runat="server" Text="Button" />

    ASPX code behind

    protected void Page_Load(object sender, EventArgs e)
        {
          Button2.Click += new EventHandler(Button2_Click);
        }
    
        void Button2_Click(object sender, EventArgs e)
        {
          Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
          Context.Response.Flush();
          Context.Response.End();
        }
    Monday, August 16, 2010 2:38 AM
  • It doesn't work for me! Please help!

    Edited: I worked for me. Just remove all comment in custom action. Otherwise NullReferenceException will happened. Then call

     SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
    in
    dialogReturnValueCallback
    • Edited by KhanhChi Wednesday, October 13, 2010 8:13 AM find a solution
    Wednesday, October 13, 2010 3:57 AM
  • I am having a similar issue. My issue is that I cannot get a click event to fire on the server side when the button is pressed in the modal window.

    I have a div on the page that I am popping up in the modal. It's a repeater which lists out several check boxes and radio buttons. The user can select multiple options and I have 2 buttons at the bottom, one to select everything, the other to cancel it all. It all pops up fine using the showmodaldialog(options) javascript method. When a user clicks on the button to select it doesn't hit the click method in the server side code. So I can't have it run the Response actions you have listed above. Is there a particular reason why it won't let me do this?

    I have also confirmed that if I don't have it open in the modal my code works. But for some reason when it's in the modal it doesn't find the click event code in the code behind.

    I have the same issue

    I have a div on the same page, showing it using html property of the options parameter

    also i have a save button and a drop down list that has an event on selected index changed

    when i show the div without the dialog, everything works fine but when i try it inside the dialog no events is being fired

    the page flicks and the page load event is fired but the button click is not

    • Proposed as answer by game2011 Sunday, September 4, 2011 7:50 AM
    Thursday, December 16, 2010 11:49 AM
  • I face the same problem.

    Could you post some coding pls.


    I can't refresh the parent page.
    don't know where to put those coding ?

    in parent page or in popup page?

    • Edited by kyiwt Thursday, December 8, 2011 3:58 AM
    Thursday, December 8, 2011 3:58 AM
  • I spoke with the owner of the SharePoint Dialog Platform and he provided the following information:

    The solution is to write JS directly to the http Response object that will close the dialog. In order to have it refresh the page when the dialog is closed, you can pass in SP.UI.ModalDialog.RefreshPage as the dialogReturnValueCallback function.

    The server-side code should look like:
    Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
    Context.Response.Flush();
    Context.Response.End();
    

    Hope this helps

     


    Jonathan Kern :: UX Developer :: SharePoint Foundation


    After server-side code runs, we wanted to display status message as well. So I tried following code, but does not seem to work.

                Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup(); SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, '1');</script>");
                Context.Response.Flush();
                Context.Response.End();

     


    Bhavesh
    Monday, January 23, 2012 10:35 AM
  • Managed to solve the problem using following JS

     

    <script type='text/javascript'>window.frameElement.commonModalDialogClose(1, 'New call was successfully logged.');</script>


    Bhavesh
    • Proposed as answer by bhav27 Monday, January 23, 2012 12:53 PM
    Monday, January 23, 2012 12:53 PM