Индекс находился за пределами массива
public List<Item> GetTaggedRelatedItems() { List<Item> taggedRelatedItem = new List<Item>(); try { const int totalItemCount = 4; var scContext = new SitecoreContext(); List<Item> finalItems = new List<Item>(); //Fetching the pubs and reports landing page from rendering para var topicField = Sitecore.Context.Item.Fields["Topic"]; if (topicField != null) { string[] housingPhaseTags = topicField.Value.Split(new char[] { '|' }); foreach (var tag in housingPhaseTags) { // Check for tags which are added as |Tag1|Tag2 instead of Tag1|Tag2 if (!string.IsNullOrWhiteSpace(tag)) { var tagItem = Sitecore.Context.Database.GetItem(ID.Parse(tag)); if (tagItem != null && tagItem.Fields["RootNode"] != null) { var rootNode = Sitecore.Context.Database.GetItem(ID.Parse(tagItem.Fields["RootNode"].Value)); if (rootNode != null) { //var relatedItems = rootNode.Axes.GetDescendants().Where(x => x.Visualization.GetLayout(Sitecore.Context.Device) != null); var childItems = contentSearchAPI.GetIndexForRootItem(rootNode, ID.Parse(Guid.Empty)); if (childItems != null) { var relatedItems = childItems.Where(x => x != null && x.Visualization.GetLayout(Sitecore.Context.Device) != null); //## Content Search API ## var housingPhaseItems = relatedItems.Where(x => x.Fields["Topic"].Value.Contains(tag)).ToList(); taggedRelatedItem.AddRange(housingPhaseItems); //Fetch the topic or subject tag in the given navigational bucket var topicTags = Context.Item.Fields["Tags"]; if (topicTags != null && !string.IsNullOrWhiteSpace(topicTags.Value)) { //Hack to make the tags work as the content migration script introduced extra | at the begining of the tags field. // So picking the [1] item as opposed [0] item. var firstTopicTag = topicTags.Value.Split(new char[] { '|' })[1]; finalItems = housingPhaseItems.Where(x => x.Fields["Tags"].Value.Contains(firstTopicTag)).ToList(); // Fetch remaining values from other topic or subject tags if (finalItems.Count == 0 || finalItems.Count < 4) finalItems.AddRange(GetSecondaryRelatedItem(housingPhaseItems, totalItemCount - finalItems.Count)); finalItems.ForEach(x => taggedRelatedItem.Add(scContext.Cast<Item>(x))); } } else { Sitecore.Diagnostics.Log.Info("HCWebHelper -> GetTaggedRelatedItems - No Items(s) found from Index", this); } } } } } } } catch (Exception ex) { StackTrace stackTrace = new StackTrace(ex, true); var topFrame = stackTrace.GetFrame(1); var caller = topFrame != null ? topFrame.GetMethod()?.Name : string.Empty; var line = topFrame != null ? topFrame.GetFileLineNumber().ToString() : string.Empty; var exString = string.Format(@"Message : {0}, Caller: {1}, Line: {2}, StackTrace: {3}", ex.Message, caller, line, ex.StackTrace); Sitecore.Diagnostics.Log.Error(exString, this); } //Incase of empty taggedRelatedItem list it should return empty list<item>() return (taggedRelatedItem != null && taggedRelatedItem.Count >= 4) ? taggedRelatedItem.Take(4).ToList() : new List<Item>(); }
Что я уже пробовал:
Мой код выглядит так, как показано на рисунке, но я получаю ошибку журнала "индекс был вне границы массива"