Anders G. Nordby

Lead Consultant at Itera

Tag Archives: DDS

Hiding seen ChangeInfo in an EPiServer CMS 6 R2 Solution

This post is mostly a reminder to myself about how to achieve this.

The task
In addition to marking a page as changed, the customer wanted the editors to be able to show the users a description of what is new/updated. The customer also wanted users to be able to mark the message as seen. However, when another change is made, the (new) message should be shown to all users again. The CMS is an intranet, it uses PageTypeBuilder, and all users are logged in via ActiveDirectory.

I first tried to use PageObjects for this, but didn’t quite get it to work properly. When I switched to storing everything in Dynamic Data Store (DDS) myself, things worked a lot better. Since I spent some time figuring out how to do this properly, I thought it would be a good idea to document my solution here.

My solution
First I created a class to hold the list of users who have seen and clicked away the message:

public class ChangeSeen : IDynamicData
{
    public Identity Id { get; set; }
    public List<string> SeenBy { get; set; }
}

A small helper method:

private static DynamicDataStore GetChangeSeenStore()
{
	return DynamicDataStoreFactory.Instance.GetStore(typeof (ChangeSeen)) ??
		   DynamicDataStoreFactory.Instance.CreateStore(typeof (ChangeSeen));
}

During PageLoad of the usercontrol that will show the message (or not), I check if the current user is in the list of people who has clicked the message away:

protected void Page_Load(object sender, EventArgs e)
{
	Visible = false;

	if (string.IsNullOrEmpty(CurrentPage.ChangeInfo))
		return;

	var store = GetChangeSeenStore();
	var seen = store.Load<ChangeSeen>(Identity.NewIdentity(CurrentPage.PageGuid));
	if (seen != null && seen.SeenBy.Contains(PrincipalInfo.CurrentPrincipal.Identity.Name))
		return;

	Visible = true;
}

The button handler for the button to remove the message:

protected void SeenClicked(object sender, EventArgs e)
{
	var store = GetChangeSeenStore();
	var seen = store.Load<ChangeSeen>(Identity.NewIdentity(CurrentPage.PageGuid)); 
	if (seen != null)
	{
		if (!seen.SeenBy.Contains(PrincipalInfo.CurrentPrincipal.Identity.Name))
			seen.SeenBy.Add(PrincipalInfo.CurrentPrincipal.Identity.Name);
	}
	else
	{
		var seenBy = new List<string> {PrincipalInfo.CurrentPrincipal.Identity.Name};
		seen = new ChangeSeen {Id = Identity.NewIdentity(CurrentPage.PageGuid), SeenBy = seenBy};
	}
	store.Save(seen);

	Response.Redirect(Helpers.FriendlyURL.GetFriendlyURL(CurrentPage));
}

And finally, in Global.asax, I remove the list of people from a page when the page is updated. After all, then there will be changes (and hopefully a new ChangeInfo message):

protected void Application_Start(Object sender, EventArgs e)
{
	DataFactory.Instance.PublishingPage += PublishingPage;
}

private static void PublishingPage(object sender, EPiServer.PageEventArgs e)
{
	if (e.Page is PageTypes.BaseEditorialPage && e.Page.PageGuid != Guid.Empty)
	{
		var dynamicDataStore = GetChangeSeenStore();
		var identity = Identity.NewIdentity(e.Page.PageGuid);
		if (dynamicDataStore.Load(identity) != null)
			dynamicDataStore.Delete(identity);
	}
}
Advertisements