In my DW8 version I can do this:
/// <summary>
/// Looks up the Url for the Product in a site (Area) having searched through all the eCom_Catalog modules in the site
/// finding the first availability of the Product in a Group attached to a correct eCom_Catalog module. Returns the Url to the module to find the Product.
/// </summary>
/// <param name="Product">The Product Object you need the Url to.</param>
/// <param name="areaId">The Area.ID from the Page.</param>
/// <returns>The Url.</returns>
public static string GetProductUrl(Product product, int areaId)
{
IEnumerable<Paragraph> areaModuleParagraphs = Paragraph.GetParagraphsByModuleName("eCom_Catalog")
.Where(n => n.Page.AreaID == areaId && !n.Page.Hidden && n.Page.Active); // There are 130 in Area 1
Dynamicweb.eCommerce.Products.GroupCollection productGroups = product.Groups;
var productPageUrl = string.Empty;
var productPageLink = string.Empty;
foreach (var paragraph in areaModuleParagraphs)
{
Dynamicweb.Properties properties = Dynamicweb.Base.GetParagraphModuleSettings(paragraph.ID);
ProductsAndGroupsHandler productGroupSelector = new ProductsAndGroupsHandler(properties["ProductAndGroupsSelector"]);
foreach (var group in productGroups)
{
if (productGroupSelector.GroupsSelected.Contains(group))
{
// DW throws a null ref exception at .GetFriendlyUrl if there is no pageview loaded in context
Dynamicweb.Frontend.PageView pw = Dynamicweb.Frontend.PageView.GetPageviewByPageID(paragraph.PageID);
pw.Load();
if (!string.IsNullOrEmpty(product.VariantID))
{
productPageUrl = "Default.aspx?ID=" + paragraph.PageID + "&ProductID=" + product.ID + "&VariantID=" + product.VariantID;
}
else
{
productPageUrl = "Default.aspx?ID=" + paragraph.PageID + "&ProductID=" + product.ID;
}
productPageLink = Dynamicweb.Frontend.SearchEngineFriendlyURLs.GetFriendlyUrl(productPageUrl);
if (!String.IsNullOrEmpty(productPageLink))
{
return productPageLink;
}
}
}
}
return productPageLink;
}
Its a bit verbose and convaluted but it works well. (Does not work for Smart Searches)
In 9 I try:
List<Paragraph> areaModuleParagraphs = Dynamicweb.Extensibility.ServiceLocator.Current.GetParagraphService().GetParagraphsByModuleName("eCom_Catalog")
.Where(n => n.Page.AreaId == areaId && !n.Page.Hidden && n.Page.Active).ToList();
But there are no Groups selected in productGroupSelector.GroupsSelected. "{all}"
Could my approach be better here or am I missing something new in DW9??
Rgds
Kevin