public static Sitecore.Collections.HistoryEntryCollection getUniqueLatestVersionFromHistoryCollection(Sitecore.Collections.HistoryEntryCollection data) { Sitecore.Collections.HistoryEntryCollection hec = null; if (data != null && data.Count > 0) { hec = new Sitecore.Collections.HistoryEntryCollection(); //This will find out unique items in history collection var unique = data.DistinctBy(x => x.ItemId); foreach (var item in unique) { //This will found those items with same ID in the original data collection List <Sitecore.Data.Engines.HistoryEntry> sameItems = (from he in data where he.ItemId == item.ItemId select he).ToList <Sitecore.Data.Engines.HistoryEntry>(); //This will find out the latest version possible in the sameItems collection Sitecore.Data.Engines.HistoryEntry historyEntry = sameItems.Aggregate((i1, i2) => i1.ItemVersion.Number > i2.ItemVersion.Number ? i1 : i2); //Finally add the value to real history collection if (historyEntry != null) { hec.Add(historyEntry); } } } return(hec); }
private static Item getHistoryItem(Sitecore.Data.Engines.HistoryEntry item, string dbname, int version, string language = "en", string path = "") { //If History entry item is null then return null if (item == null || dbname == null) { return(null); } if (language != null && language.Length > 0) { //if language doesn't match then return null if (!item.ItemLanguage.Name.Equals(language, StringComparison.CurrentCultureIgnoreCase)) { return(null); } } //if version doesn't match then return null if (item.ItemVersion.Number != version) { return(null); } if (path != null && path.Length > 0) { string compPath = item.ItemPath + @"/"; //if path doesn't fall under given path which means location is not correct then return null if (!compPath.Contains(path)) { return(null); } } return(GetItem(item.ItemId.Guid.ToString(), dbname, language, version)); }