Sorting users in the Sitefinity Backend
Sitefinity has a friendly user interface for all kinds of activities. One of...
Recently I wanted to use the Archive control in one of my projects. The requirement of the customer was that they would like to filter news items based on years. Now they have an archive of like six years, so they also wanted to limit these years to show only the two latest years.
Recently I wanted to use the Archive control in one of my projects. The requirement of the customer was that they would like to filter news items based on years. Now they have an archive of like six years, so they also wanted to limit these years to show only the two latest years.
So I ran into some troubles regarding these requirements.
I'll explain how to solve these two issues.
When using a pager on the NewsWidget, the filtering on the year was removed.
To solve this issue it was just a matter of changing the UrlEvaluationMode of the NewsView widget from 'UrlPath' to 'QueryString'. Now the urls look a bit more unfriendly, but at least you can page through your selection.
I couldn't configure the Archive control to limit the item count
For this issue to solve, I created a new control and inherited from the Telerik.Sitefinity.Web.UI.PublicControls.ArchiveControl.
I created a property to actually let the administrator of the site adjust the limit of the items:
/// <summary>
/// Get and Set the Limit on the ArchiveItems
/// </summary>
public int Limit {
get { return _limit; }
set { _limit = value; }
}
After that I override the DataBindArchiveRepeater method, to adjust the filter to the datasource, like this:
/// <summary>
/// Override the DataBindArchiveRepeater method
/// </summary>
/// <param name="items"></param>
public override void DataBindArchiveRepeater(List<Telerik.Sitefinity.Modules.GenericContent.Archive.ArchiveItem> items) {
if (ArchiveRepeater == null) return;
ArchiveRepeater.DataSource = items.OrderByDescending(x => x.Date).Take(Limit);
ArchiveRepeater.ItemDataBound += ArchiveRepeaterOnItemDataBound;
ArchiveRepeater.DataBind();
}
I also override the ArchiveRepeaterOnItemDataBound event, but that was just to have some modifications on how the rendering was happening for each item inside the repeater:
private void ArchiveRepeaterOnItemDataBound (object sender, RepeaterItemEventArgs e) {
if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
var dataItem = e.Item.DataItem as ArchiveItem;
var hyperLink = e.Item.FindControl ("linkArchive") as HyperLink;
if (hyperLink == null) return;
if (!this.IsDesignMode ()) {
if (dataItem != null) {
hyperLink.NavigateUrl = this.ResolveUrl (dataItem.Date);
if (IsDateInRange (dataItem.Date)) {
hyperLink.CssClass = "sfSel";
}
}
}
if (dataItem != null) hyperLink.Text = ResolveDisplayText (dataItem.Date);
if (!ShowItemCount) return;
var hyperLink1 = hyperLink;
if (dataItem != null)
hyperLink1.Text = string.Concat (hyperLink1.Text, string.Format (" <span class='sfCount'>({0})</span>", dataItem.ItemsCount));
}
So, with help of Sitefinity Thunder I added the new control to the toolbox, builded my project and the widget now shows the latest 3 years inside the archive control.
Thanks Boyan Barnev for support on this.
Entrepreneur, Senior Software Engineer .NET, Sitefinity Solution specialist, Orchard CMS enthusiast, Product Owner
All Author PostsSitefinity has a friendly user interface for all kinds of activities. One of...
Make sure your Sitefinity embedded resources are being updated during deployment