Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. <Root>/sitecore/content/Articles</Root>
  2.  
  3. public class Article : SearchResultItem
  4. {
  5. [IndexField("_uniqueid")]
  6. public string ItemPath { get; set; }
  7.  
  8. [IndexField("_group")]
  9. public string Id { get; set; }
  10.  
  11. [IndexField("title")]
  12. public string Title { get; set; }
  13.  
  14. [IndexField("publisheddate")]
  15. public DateTime PublishedDate { get; set; }
  16.  
  17. [IndexField("summary")]
  18. public string Summary { get; set; }
  19.  
  20. }
  21.  
  22. list.Where(c => c.PublishedDate.Year == 2017).ToList();
  23.  
  24. public class ComputedYearField: IComputedIndexField
  25.  
  26. {
  27. public string FieldName { get; set; }
  28. public string ReturnType { get; set; }
  29.  
  30. public object ComputeFieldValue(IIndexable indexable) {
  31.  
  32. Item item = (indexable as SitecoreIndexableItem);
  33.  
  34. if (item != null)
  35. {
  36.  
  37. // check if item is an Article
  38. if(item.TemplateID == "YOURARTICLETEMPLATEID")
  39. {
  40. // extract the year from PublishDate field
  41. var dateField = (DateField)item.Fields["PublishedDate"];
  42. var itemDate = Sitecore.DateUtil.IsoDateToDateTime(dateField.Value);
  43.  
  44. return itemDate.Year
  45. }
  46. }
  47. return null;
  48. }
  49.  
  50. }
  51.  
  52. <fields hint="raw:AddComputedIndexField">
  53. <field fieldName="ComputedYear" returnType="int">
  54. MyProjectNamespace.ComputedYearField,MyProjectNamespace
  55. </field>
  56. </fields>
  57.  
  58. list.Where(c => c.ComputedYear == 2017).ToList();
  59.  
  60. var dateStart = new DateTime(year, 1, 1);
  61. var dateEnd = new DateTime(year, 12, DateTime.DaysInMonth(year,12));
  62.  
  63. list.Where(x => x.Date.Between(dateStart, dateEnd, Inclusion.Both));
  64. // or
  65. // list.Where(x => x.Date>=dateStart && x.Date<=dateEnd);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement