Developer forum

Forum » Development » Get groups sorted via API - Ecom

Get groups sorted via API - Ecom

Jan Sangill
Reply

Hi,
You have this method:
Dynamicweb.Ecommerce.Products.Group.GetAllGroups();

How would one go about getting groups in sorted fashion? Like the menu would output it.

//jan


Replies

 
Nicolai Pedersen
Reply

Hi Jan

Groups are a many-to-many relation where the same group can have more than one parent and each of those relationships has its own sort value. Those values can be found in EcomGroupRelations table in the database.

Therefore you need some kind of tree context in order to sort the groups.

If you have a Group object, you can simply ask for Subgroups property - which is sorted.

Alternatively you can use the ParentGroupSortOrderComparer to sort a collection. But it would be the same thing...

Dim relationsByParent As GroupRelationCollection = GroupRelation.GroupRelationsByParentId(parentId)
CType(Items, List(Of Group)).Sort(New ParentGroupSortOrderComparer(parentId, relationsByParent))

BR Nicolai

 
Jan Sangill
Reply

Hi Nikolai,

I get the subgroups are sorted. But how would one get the topgroups sorted via API then?
Any easy method there or?

I tried simply putting 0 in as "parentid" - but no luck. Perhaps I am just missing the correct format for the parentid for topgroups?

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply
This post has been marked as an answer

Hi Jan,

You can get the top level groups from the shop.

Try something like this...

var shopId = Dynamicweb.Frontend.PageView.Current().Area.EcomShopId;
 
var shop = !string.IsNullOrEmpty(shopId) ? Dynamicweb.Ecommerce.Services.Shops.GetShop(shopId) : Dynamicweb.Ecommerce.Services.Shops.GetDefaultShop();
 
if (shop != null)
{
    var topLevelGroups = shop.TopLevelGroups;
 
    foreach (var topLevelGroup in topLevelGroups)
    {
        var subGroups = topLevelGroup.Subgroups;
    }
}

Best regards,
Morten

Votes for this answer: 1
 
Jan Sangill
Reply

Perfect Morten.

I have it sorted now:>

 

You must be logged in to post in the forum