Hi Matt,
You definitely can remove blocks from a content area programatically, you probably just need to create a writeable clone of the current page, which is as easy as:
currentPage.CreateWritableClone()
However, if I correctly understand what you're trying to do your approach seems a little complex and potentially confusing for editors. Have you considered using personalization and visitor groups? Out-of-the-box Episerver has a "Time of Day" criteria which means editors should be able to do this without you having to write any code.
Take a look here and see if it fits your requirements: https://webhelp.episerver.com/16-4/cms-admin/visitor-groups.htm
Hi Jake,
Thanks for the reply, the Out-of-the-box tool is a good way, but the requirement is quite complex, for example, if there are two messages need to display but we can only display one message at the time, so I also need to compare the priority or the created time of the message. and also has other specific requirements. So I need to create this party by myself.
I will try the createWritableClone method, thanks again.
Hi Matt,
Rather than dynamically removing items from the content area yourself, I'd take a look at personalisation using visitor groups. If your requirements are as simple as showing a different item for each day of the week then there's already a personalisation criterion you could use. If the requirements are more complex, it's pretty easy to write new criteria or to combine ready-built ones to create more comprehensive segmentation. By taking this approach you don't really need to do anything in the controller and the logic for displaying the items can be more easily adjusted as requirements evolve.
Hi Matt,
I understood that you only want to filter messages by today in content area in controller to display in view mode, right? You do not want to remove permanently from content area, right?
If it is true, you can use personalization as Jake's answer or you can check the condition and rendering in html view like this:
@foreach (var item in Model.BottomContentArea.FilteredItems.Where([item need to show today]))
{
Html.RenderContentData(item.GetContent(), true);
}
Hello
I would recommend you to create your own criteria instead and put the logic in that.
Read more here on how to do that
Maybe I'm asking a stupid question but why do you need this to be so complex. Can't you just dynamically hide or show the message for your header in code? If you want the text customizable just pop it in a site setting or page settings.
E.g pop the messages in a site settings you define with the 7 messages, then just output the correct one in the header. Unless this requirement for which message is shown can change I think using personalization or content areas is way overkill
There is a requirement to show a different message depending on the day of the week in the page's header area.
For example, display "Hello Monday" on Monday, display "Hi Tuesday" on Thursday.
So I created a content area and added all messages to it and try to remove unnecessary message items in the controller.
like:
if(items[i] no need to show today){
currentPage.NotificationBlock.Items.RemoveAt(i)
}
but got error says "it's readonly property cannot remove it".
Is there some way we can do that?