Adjust the archive control: fix for paging & limit items

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.

  • May 31, 2012

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.

  1. When using a pager on the NewsWidget, the filtering on the year was removed.
  2. I couldn't configure the Archive control to limit the item count

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.  


Image Description

Daniel Plomp

Entrepreneur, Senior Software Engineer .NET, Sitefinity Solution specialist, Orchard CMS enthusiast, Product Owner

All Author Posts

Got another minute? Check out:

Image Description

Call us

+31 (0) 36-2000196
Image Description

Address

Grote Beer 57, 3893 DJ Zeewolde
NLThe Netherlands

Please sign in

Signin to manage your account.

Do not have an account? Signup

OR