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 aIsRead
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 underlyingSPListItem
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