Developer forum

Forum » Development » Trace back parent group

Trace back parent group

Dmitrij Jazel
Reply

Hi everybody,

Trying to come up with a recursive method, to iterate throgh parent tree og a group.

I have a structure of 4 levels in ecom groups, and I need to trace back the parent of each group in the path.

what I have so far:

@helper RenderParentEcomNav(Dynamicweb.eCommerce.Products.Group group)
{
    if(group.ParentGroupsDefaultLanguage != null){
        var parent = Dynamicweb.eCommerce.Products.Group.GetGroupByID(group.PrimaryParentGroupID);
        if (parent != null)
        {
            @RenderParentEcomNav(parent)
        }else{
            <text><li>Shop-root</li></text>
        }
    }
    <li>@group.ID</li>
}

But am getting null or 'emptyspace' when I am calling the parent of my parent - therefore null pointer exceltion.

It's because after putting in an original group - it gets parent - but than next time that parent.PrimaryParentGroupID is null.

Is that the way to get the group parent, or should I try something else,

I also tryed:

group.ParentGroups[0].ID

but same results.

 

Any suggestions?

/Dmitrij


Replies

 
Dmitrij Jazel
Reply

if I change:

group.PrimaryParentGroupID

to:

group.ParentGroupsDefaultLanguage[0].ID

 

​I am getting index out of bound exception, but that's because I can't call for [0] on the null mostlikely but, still is there a better solution?

/Dmitrij

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Check if ParentGroupsDefaultLanguage is null, then check if ParentGroupsDefaultLanguage[0] is null before you get the ID?

Imar

 
Dmitrij Jazel
Reply

Hi Imar,

Ok, I will try that, meanwhile I am trying, just a quick question, what if one if them is Null? I mean, every group (like every product) should have atleast 1 parent, or 1 group.

You have the link in DB (EcomGroupRelations), so you should be a very easy and attribute/method.

 
Dmitrij Jazel
Reply

Hi Imar,

Thanks for the tips, and I also had a tipe-mistake.

This is what I have so far, and it works perfectly :)

@helper RenderParentEcomNav(Dynamicweb.eCommerce.Products.Group group)
{
    if (group.ParentGroupsDefaultLanguage != null && group.ParentGroupsDefaultLanguage.Count > 0)
    {
        string parent_id = group.ParentGroupsDefaultLanguage[0].ID;
        Dynamicweb.eCommerce.Products.Group parent = Dynamicweb.eCommerce.Products.Group.GetGroupByID(parent_id);
        if (parent != null)
        {
            <text>@RenderParentEcomNav(parent)</text>
        }
        else
        {
            <text><li>Shop-root</li></text>
        }
    }
    <li>@group.ID</li>
}

/Dmitrij

 

You must be logged in to post in the forum