Hi,
I was wondering if any of you out there knows if it is possible to change a newslist by using either an extender or subscriber?
What I want to do is e.g. to change the sorting of the news items in a news list.
// René
Hi,
I was wondering if any of you out there knows if it is possible to change a newslist by using either an extender or subscriber?
What I want to do is e.g. to change the sorting of the news items in a news list.
// René
Hi Rene,
It depends on which News module you use. If you're using NewsV2 (the one not obsoleted with DW8) then you can subscribe to this notification: Dynamicweb.Modules.News.Notifications.Frontend.AfterSelectData. You'll need to add an assembly reference to the Dynamicweb.Modules assembly.
This notification is broadcast for all News actions, e.g. detail view and edit. In order to determine which action happened, do a typeof on the Data property of the args object:
[Subscribe(Dynamicweb.Modules.News.Notifications.Frontend.AfterSelectData)] public class NewsListObserver : NotificationSubscriber { public override void OnNotify(string notification, NotificationArgs args) { var newsArgs = (Dynamicweb.Modules.News.Extensibility.FrontendDataNotificationArgs)args; // Determine type of action if (newsArgs.Data.GetType() == typeof(Dynamicweb.Modules.News.NewsItemCollection)) { var newsList = (Dynamicweb.Modules.News.NewsItemCollection)newsArgs.Data; // TODO: Implement custom sorting on newsList collection } } }
Hope this helps :)
- Jeppe
Hi Jeppe,
Thank you very much! I'm using the NewsV2, so this was exactly what I needed :-)
// René
Hi Jeppe,
I have an extra question for you :-) Is there anyway I can get the html of the template used for the news list? I don't want my code to run on every news list, but only if something (e.g. <!--RandonNewsSort-->) is in the template. If this is not possible, then I would like to know if there is a different approach to use? :-)
// René
Figured it out :-)
I can get the paragraph and then the template for the attached module. On the template object i have the .Html property with all the HTML from the template:
Paragraph p = Paragraph.GetParagraphById(item.ModuleInstance.ParagraphId);
string moduleTemplatePath = "NewsV2/List/" + p.ModuleProperties["NewsParagraphListTemplate"];
Template t = new Template(moduleTemplatePath);
if (t.Html.Contains("<!--NewsRandomSort-->")) {
// Do stuff here...
}
You must be logged in to post in the forum