Developer forum

Forum » Ecommerce - Standard features » Is there a way to list usergroups (ID and Name) in the ecom product template

Is there a way to list usergroups (ID and Name) in the ecom product template

Thomas Jensen
Reply

Is there a way to list usergroups (ID and Name) in the ecom product template


GetGlobalValue("Global:Extranet.Groups") gives me a list of user(group)IDs eg. "23,432,1212"
but there is no easy way to finde the the id for a specific usergroup in admin
I can finde it in SQL and with a Data List, not an easy use solutions


So how can I get the group names in the template ?


I have tried 
Dynamicweb.Security.UserManagement.User.GetUserByID(23);
it dos not fail but I dont get any data out

Dynamicweb.Content.Items.ItemManager.Storage.GetById(Dynamicweb.Security.UserManagement.User.ItemType,"23");
this fails


Datalist app
I can make the list or make a function, 
but getting it in to the product template with RenderParagraphContent will not work
(need to use the data server side) I can use this method client side 


Repository, QueryPublisher
have not got it to give me user data yet
but I think that it will end up in the same issue as width the datalist app

 

Regards Thomas


Replies

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

Hi Thomas,

In front end you can use the Extranet app to render a list of groups. You can then use snippets to move the content into the product template output.

If you prefer to use the API then there are several options. Here is a few examples...

// get groups for a specific user
int userId = 123;
var user = Dynamicweb.Security.UserManagement.User.GetUserByID(userId);
if (user != null)
{
    var groups = user.Groups;
}
// get groups for the current user
var user = Dynamicweb.Security.UserManagement.User.GetCurrentExtranetUser();
if (user != null)
{
    var groups = user.Groups;
}
// get groups from a collection of group ids
int[] groupIds = new[] { 23, 432, 1212 };
var groups = Dynamicweb.Security.UserManagement.Group.GetGroupsByID(groupIds);
​// get groups from a string with comma separated ids
string groupIds = "23,432,1212";
var groups = Dynamicweb.Security.UserManagement.Group.GetGroupsFromString(groupIds);
// Once you have the groups you can render them any way you want...
@foreach (var group in groups)
{
    <p>@group.Name (@group.ID)</p>
}

These are just examples. Try to avoid hard coded ids in your template. It will come back and haunt you later :)

Best regards,
Morten

Votes for this answer: 1
 
Thomas Jensen
Reply

Hi Morten

I only use hard coded, for testing
I will get the group list form the current user @GetGlobalValue("Global:Extranet.Groups")
And yes the ID I use to test do exists

 

Testing your examples:
"get groups from a collection of group ids" or "get groups from a string with comma separated ids"

If I print @groups I get this text "Dynamicweb.Security.UserManagement.GroupCollection"

If I do @foreach (var group in groups){<p>Value: @group.Name(@group.ID)</p>}
I dont get anyting out, not even the text Value:
So no loop items

I have added:
@using Dynamicweb.Security;
@using Dynamicweb.Security.UserManagement;
To the top of the template, but still no data out of the foreach

 

This is on a version 9.4.18, dose that make a diference?

It looks like I have to use the client-side version for now

 

Regards Thomas

 

 

You must be logged in to post in the forum