Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Get a linked product url

Read my blog here

public void GetProductUrl(ProductContent product, ContentReference catalogLink, out string path)
{
path = product.RouteSegment;
ContentReference parentLink = product.ParentLink;
if (catalogLink.CompareToIgnoreWorkID(contentReference: parentLink))
{
return;
}
ContentReference contentLink = product.ContentLink;
int objectId = this.referenceConverter.GetObjectId(contentLink: catalogLink);
NodeContent content;
while (this.contentLoader.TryGet(contentLink: parentLink, content: out content))
{
if (content.CatalogId != objectId)
{
NodeContent linkedParentNode = this.GetLinkedParentNode(
contentLink: contentLink,
catalogLink: catalogLink);
if (linkedParentNode == null)
{
return;
}
content = linkedParentNode;
contentLink = linkedParentNode.ContentLink;
}
path = content.RouteSegment + "/" + path;
parentLink = content.ParentLink;
if (this.referenceConverter.GetContentType(contentLink: parentLink) == CatalogContentType.Catalog)
{
break;
}
}
}
private NodeContent GetLinkedParentNode(ContentReference contentLink, ContentReference catalogLink)
{
List<ContentReference> source = this.relationRepository.GetParents<NodeRelation>(childLink: contentLink)
.Where(r => r.TargetCatalog.CompareToIgnoreWorkID(contentReference: catalogLink)).Select(r => r.Parent)
.ToList();
if (!source.Any())
{
return null;
}
LoaderOptions settings =
new LoaderOptions { new LanguageLoaderOption { Language = ContentLanguage.PreferredCulture } };
return this.contentLoader.GetItems(contentLinks: source, settings: settings).OfType<NodeContent>()
.FirstOrDefault();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment