5

I have customized the display form on one of my list.

On a webpart page, I have drop a view to this list. When I click on the title of my item, the custom disp form is shown as expected in a modal dialog.

I would like to force the refresh of the view on my webpart page as soon as the dialog is closed. How can I achieve that ?

I've tried to add this script in my custom form :

SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);

But it fails (an obscure javascript error).

All exemples I've found on the web to get this working rely on changing the parent's page script, not the child.

Any idea?

PS: an ajax refresh is prefered, as my webpart is an OOB webpart. If reloading the whole page is needed, I can live with that too.

[Edit], I've also tried with this :

window.onload = ExecuteOrDelayUntilScriptLoaded(function(){SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);}, 'sp.js');

This fires the refresh of the child dialog, not the parent.

[Edit2]: As it seems to be unclear, I highlith the fact that I'm not generating myself the code to open the dialog. I use a standard and OOB list view webpart, that is opening the dispform.Aspx form of my list. And I added my webpart in this page. This is the only entry point where I can add code.

[Edit3]: here is the full code of the webpart I added in my dispform.aspx :

[ToolboxItemAttribute(false)]
public class MessageSetRead : WebPart
{
    protected override void OnInit(EventArgs e)
    {
        try
        {
            var listItem = SPContext.Current.ListItem;

            // TODO: ensure listitem is a message and exists
            var isReadField = listItem.Fields.GetFieldByInternalName("IsRead");
            var isRead = (bool)listItem[isReadField.Id];
            var recipientField = listItem.Fields.GetFieldByInternalName("AssignedTo");
            var recipientStr = (string)listItem[recipientField.Id];

            var principalId = new SPFieldUserValue(SPContext.Current.Web, recipientStr).LookupId;
            var web = SPContext.Current.Web;
            var isCurrentUserRecipient = string.IsNullOrEmpty(recipientStr) ?
                false :
                principalId == web.CurrentUser.ID;

            var isCurrentUserMemberOf = string.IsNullOrEmpty(recipientStr) ?
                false :
                web.CurrentUser.Groups.Cast<SPGroup>().Any(g => g.ID == principalId);


            if (!isRead && (isCurrentUserRecipient || isCurrentUserMemberOf))
            {
                using (web.CreateUnsafeUpdateAllowedContext())
                {
                    SPUtility.ValidateFormDigest();
                    web.RunWithElevatedPrivileges((SPWeb elevatedWeb) =>
                    {
                        using (elevatedWeb.CreateUnsafeUpdateAllowedContext()) { 
                        var elevatedList = elevatedWeb.Lists[SPContext.Current.ListId];
                        var elevatedListItem = elevatedList.Items.GetItemById(SPContext.Current.ItemId);
                        elevatedListItem[isReadField.Id] = true;
                        elevatedListItem.SystemUpdate(false);}
                    });
                }
            }
        }
        catch (Exception exc)
        {

            this.Controls.Clear();
            var errorMessage = new Label();
            errorMessage.Text = HttpUtility.HtmlEncode(exc.ToString());
            errorMessage.Style[HtmlTextWriterStyle.WhiteSpace] = "no-wrap";
            errorMessage.Style[HtmlTextWriterStyle.Color] = "red";
            this.Controls.Add(errorMessage);
        }
    }
}

I highlighted the word full because this is the only code I use in my feature.

So the summarize what I have :

  • a custom content type Message, with a IsRead boolean column
  • a list instance of this content type also called Messages
  • an oob of the box list view webpart (XsltListViewWebPart under the hood)
  • a custom Xslt to show unread items bold
  • an oob dispform.aspx
  • a custom webpart (the one to which the code below belongs) that sets the flag IsRead on the underlying SPListItem found in the context
  • the webpart is added to the page manually once

Everything is working fine, related to the field's value update, but when the user display the item's details from the home page, the page is not refreshed when the dialog close. I think it's expected because OOB, there's not need to refresh an item when showing dispform of the item

8
  • If I understands this; when you close your custom dispform you want to reload the parent page, wouldnt it work to add window.parent.location.reload(true); to the onclick events in the form? Commented Jan 3, 2013 at 12:32
  • 1
    @AndersAune: actually, the webpart I've added only update the listitem values. The main webpart of the form is the oob webpart that shows the item's data, and then, I don't generate myself the form's buttons. I think the only way I have to interact with the modal dialog is to inject some javascript at page load. I believed there was a way to override the modal behavior to act like an edit form dialog, and not like a display form dialog.
    – Steve B
    Commented Jan 3, 2013 at 13:12
  • Yeah, you would probably have to change the behavior of the buttons in the form by injecting JavaScript on load. Commented Jan 3, 2013 at 13:23
  • @AndersAune: I've added my actual full code fyi. Hacking the standard button behavior may be a solution, but I've no idea where to start
    – Steve B
    Commented Jan 3, 2013 at 13:23
  • I am facing the same issue, did you find a solution to your problem ?
    – user15959
    Commented Mar 28, 2013 at 17:05

6 Answers 6

4

Use

options.dialogReturnValueCallback=RefreshOnDialogClose;

it will work :)

function ShowDialog(url) {
    var options = SP.UI.$create_DialogOptions();
    options.url = url;
    options.width = 900;
    options.height = 700;
    options.dialogReturnValueCallback=RefreshOnDialogClose;
    SP.UI.ModalDialog.showModalDialog(options);
}
1
  • Vivek, did you read the question? I'm not opening myself the dialog. It's a OOB list view that pops up a dialog when I open a list item. I only have a webpart in this dialog where I can add code.
    – Steve B
    Commented Jan 3, 2013 at 8:42
2

Hi you should try this it's working for me Important part is dialogReturnValueCallback: scallback

 function ShowModal() {
    ExecuteOrDelayUntilScriptLoaded(function () {
        var options = {
            url: 'servername/webpart.aspx',
            tite: 'Add Vendor',
            width: 800,
            height: 475,
            allowMaximize: true,
            showClose: true,
            dialogReturnValueCallback: scallback
        };
        SP.UI.ModalDialog.showModalDialog(options);
    }, 'sp.js');
 }

 function scallback(dialogResult, returnValue) {
      if (dialogResult == SP.UI.DialogResult.OK) {
           SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
      }

for close dialog use this script

 SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, passanyvaluetoparentpage);
1
  • as I said, I'm using an OOB web part. I don't generate the calling script myself.
    – Steve B
    Commented Aug 28, 2012 at 10:38
1

You want to refresh the whole page, however you also want to make sure the SP List is updated. I've added a short delay in the JS to make sure the reload fires too early.

<script type="text/javascript">
    //Setup dialog
        function modalDialog(tmpUrl)
        {
            var diaOptions = SP.UI.$create_DialogOptions();
            diaOptions.url = tmpUrl,
            diaOptions.dialogReturnValueCallback = Function.createDelegate(null, CloseCallBack);
            SP.UI.ModalDialog.showModalDialog(diaOptions);
        }
        function CloseCallBack(result, returnValue)
        {
            // On return make sure that the page is updated
            window.setTimeout(location.reload(), 500);
        }
</script>

    <a onclick="modalDialog('yourURL');return false;">
        <div class="UMR_SmallCmd">New item</div>
    </a>
0

Try to add this line in your code,

window.opener.location.reload(false);

This will reload the parent page.

0

this is answered here:

Modal box to create item

the key code is this part:

WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(contName, "", true, "", "", false, true));

look at the post i answered in the link to give you a better understanding what im going ;) . Iv tested it myself and works perfectly every time without hassel!

-1

That is pretty easy, due to MSDN Just call your JavaScript with a second Parameter from the SP.UI.ModalDialog constants:

SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, SP.UI.ModalDialog.RefreshPage);

and that's it.

3
  • The second argument is the returnval argument. According the msdn, it must be handled by the callback method passed as argument when the dialog is opened. as I said, I don't control the opener. I believe your solution won't work.
    – Steve B
    Commented Sep 3, 2014 at 11:08
  • I can tell you, it works, since I had the same scenario. I did not control the opener either, but you always have this possibility to post back this information by the returnval. Give it a try ;) Commented Sep 3, 2014 at 12:47
  • This doesn't work, so I had to downvote.
    – iOnline247
    Commented Jan 13, 2015 at 22:56

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.