1

I work on sxa Project where I have news Template, I trying to display all news items where the (News_Creation_date)field value in current year, I created search Scope to retrieve data and Rendering Variants to display fields, but I can not add condition to get data based on Current year,how can I achieve that? enter image description here

3

I had a similar requirement in which I wanted any dates that have passed today to be excluded. I created a custom rule and added to the Item Query used by Page List.

Date Rule - You could update to check for the year instead of day.

public class DateFieldPassedCondition<T> : WhenCondition<T> where T : RuleContext
{
    public string FieldName { get; set; }

    protected override bool Execute(T ruleContext)
    {
        Assert.ArgumentNotNull((object)ruleContext, nameof(ruleContext));
        var item = ruleContext.Item;
        if (item == null || string.IsNullOrEmpty(this.FieldName)) return false;

        var fieldValue = item[this.FieldName];
        var endOfToday = DateTimeProvider.GetUtcNow().Date.AddHours(23).AddMinutes(59).AddSeconds(59);
        var fieldTime = DateUtil.ParseDateTime(fieldValue, DateTime.MaxValue).Date.AddHours(23).AddMinutes(59).AddSeconds(59);

        if (fieldTime.Kind != DateTimeKind.Utc)
        {
            Log.Warn(fieldValue, (object) this);
        }

        return endOfToday > fieldTime;
    }
}

Rule entry


In this example, I want to filter out events where the end date has passed (if not empty) or when the start date has passed. Item Query

|improve this answer|||||
1

You can extend the tokens that can be used in a search (scope). It requires some custom code, but it can be very powerful. I blogged about it: https://ggullentops.blogspot.com/2019/01/extending-sxa-search-query-tokens.html

How I did it (in short):

  • create a class that overrides the ResolveSearchQueryTokensProcessor.
  • override the TokenKey
  • override the Process method: this is where you can define the logic for the query
  • add you class at the (top of the) resolveSearchQueryTokens pipeline.

The Process method works with SearchStringModels. You should search for the one you defined (with the TokenKey) and replace that with a model that can be used for an actual query, something like:

return new SearchStringModel("custom", FormattableString.Invariant(FormattableStringFactory.Create("{0}|{1}", name, value)))
  {
    Operation = "must"
  };

In your case you should create a model that matches the current year (value) to the date field. Would be easy if the date was only a year, but that is probably not the case. Not sure how to create the exact SearchStringModel for a date-year search though to be honest but you might be a (big) step closer to your solution now.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.