London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

Determine if ContentReference(Product/Variation) is a child of a node.

Vote:
0

Hi,

What would be the best way to determine if an item (by ContentReference or Code) exists in a given categorynode. So for example:

I have the contentereference of ItemA. 
I have the contentreference of NodeB.

And i want to determine if ItemA exists in NodeB or any childnode of NodeB.

Any ideas?

#248871
Feb 19, 2021 13:10
Vote:
0

One way to do that would be using IRelationRepository.GetParents<NodeEntryRelation> of the product/variant, to get its parent nodes, then use IRelationRepository.GetParents<NodeRelation> recursively (until it returns null) to see if any of its nodes, or grand-nodes matches NodeB

#248874
Feb 19, 2021 14:18
Vote:
0

What I did in one of my previous projects, I stored the complete node tree (nodes & sub-nodes) for a product as a property value and also in Episerver Find. Performance-wise its really good.

I created a property with the only getter in my product metaclass. Once the editor publishes a product,  getter automatically create a list of nodes/sub-nodes and store it as the property value. 

The next step is just to check in the code if item-A has NodeB id in the list. 

The property I created was like below 

        public IEnumerable<string> ProductCategories
        {
            get
            {
                string language = "en-GB";
                var _contentLoader = ServiceLocator.Current.GetInstance<IContentRepository>();
                var _linksRepository = ServiceLocator.Current.GetInstance<ILinksRepository>();
                var _referenceConverter = ServiceLocator.Current.GetInstance<ReferenceConverter>();

                var allRelations = _linksRepository.GetRelationsBySource(this.ContentLink);
                var categories = allRelations.OfType<NodeRelation>().ToList();
                List<CatalogContentBase> parentCategories = new List<CatalogContentBase>();
                try
                {
                    if (categories.Any())
                    {
                        foreach (var nodeRelation in categories)
                        {
                            if (nodeRelation.Target != _referenceConverter.GetRootLink())
                            {
                                CatalogContentBase parentCategory =
                                    _contentLoader.Get<CatalogContentBase>(nodeRelation.Target,
                                        new LanguageSelector(language));
                                if (parentCategory != null)
                                {
                                    parentCategories.Add(parentCategory);
                                }
                            }
                        }
                    }
                    else if (this.ParentLink != null && this.ParentLink != _referenceConverter.GetRootLink())
                    {
                        CatalogContentBase parentCategory =
                            _contentLoader.Get<CatalogContentBase>(this.ParentLink,
                                new LanguageSelector(language));
                        parentCategories.Add(parentCategory);
                    }
                }


                catch (Exception ex)
                {
                    // _log.Debug(string.Format("Failed to get categories from product {0}, Code: {1}.", productContent.Name, productContent.ContentLink), ex);
                }
                return parentCategories.Select(x => x.ContentLink.ID.ToString());
            }
        }
#248935
Edited, Feb 21, 2021 18:32
Vote:
0

That is cool approach but only worth if you need to check a lot. Also there is a problem with that is when you move a content it is not counted as publish. Your better option would be listen to the relation events 

#248965
Feb 22, 2021 0:06
Vote:
0

Indeed a cool solution Naveed, but since i don't have to check a lot i think Quan's solution will do the trick.

#249039
Feb 23, 2021 8:19
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.