Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
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
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());
}
}
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
Indeed a cool solution Naveed, but since i don't have to check a lot i think Quan's solution will do the trick.
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?