Getting group id of a group?
-
- Lieutenant (LT)
- Posts: 66
- Joined: Mon Jun 22, 2009 5:49 pm
Getting group id of a group?
I've created a group called "customers". I have code working that sets new registrations to this group by assigning the customer id of 8. I know it's 8 because I can 'view source' the admin groups page. But I don't want that "8" hard-coded. How can I get the group id of a group, given its name?
Thanks
Rick
Thanks
Rick
-
- Lieutenant (LT)
- Posts: 66
- Joined: Mon Jun 22, 2009 5:49 pm
Re: Getting group id of a group?
I tried this:
GroupCollection storeGroups = GroupDataSource.LoadForStore();
for (int i=0; i<storeGroups.Tables[0].Rows.Count; i++)
{
}
and got this error:
error CS0117: 'CommerceBuilder.Users.GroupCollection' does not contain a definition for 'Tables'
GroupCollection storeGroups = GroupDataSource.LoadForStore();
for (int i=0; i<storeGroups.Tables[0].Rows.Count; i++)
{
}
and got this error:
error CS0117: 'CommerceBuilder.Users.GroupCollection' does not contain a definition for 'Tables'
Re: Getting group id of a group?
All data in AC7 is strongly-typed, so just snag into a data class (all are defined by the API) and you're all set.
Code: Select all
Group _Group = GroupDataSource.LoadforCriteria("Name='<groupname>' ");
int _GroupId = _Group.GroupId;
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
Re: Getting group id of a group?
The correct fix for your code would beRickSilver wrote:I tried this:
GroupCollection storeGroups = GroupDataSource.LoadForStore();
for (int i=0; i<storeGroups.Tables[0].Rows.Count; i++)
{
}
and got this error:
error CS0117: 'CommerceBuilder.Users.GroupCollection' does not contain a definition for 'Tables'
Code: Select all
GroupCollection storeGroups = GroupDataSource.LoadForStore();
for (int i=0; i<storeGroups.Count; i++)
{
Group group = storeGroups[i];
int groupId = groupd.GroupId;
}
-
- Lieutenant (LT)
- Posts: 66
- Joined: Mon Jun 22, 2009 5:49 pm
Re: Getting group id of a group?
Thanks for the solutions!
-
- Lieutenant (LT)
- Posts: 66
- Joined: Mon Jun 22, 2009 5:49 pm
Re: Getting group id of a group?
AbleMods, that didn't work.
Error: 'CommerceBuilder.Users.GroupDataSource' does not contain a definition for 'LoadforCriteria'
Error: 'CommerceBuilder.Users.GroupDataSource' does not contain a definition for 'LoadforCriteria'
Re: Getting group id of a group?
Whoops I forgot to capitalize the "f" in "for" ... should be ".LoadForCriteria"RickSilver wrote:AbleMods, that didn't work.
Error: 'CommerceBuilder.Users.GroupDataSource' does not contain a definition for 'LoadforCriteria'
My Bad

Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
Re: Getting group id of a group?
If you haven't yet, you should get the free copy of Visual Studio 2008 Express from Microsoft. The Intellisense feature will show you all the options and parameters available for every data class. It's really helpful in seeing what's available on each object.
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
-
- Lieutenant (LT)
- Posts: 66
- Joined: Mon Jun 22, 2009 5:49 pm
Re: Getting group id of a group?
AbleMods, I'm afraid that doesn't work either:
Group _Group = GroupDataSource.LoadForCriteria("Name='Customer'");
Cannot implicitly convert type 'CommerceBuilder.Users.GroupCollection' to 'CommerceBuilder.Users.Group'
But this did work:
Group _Group = GroupDataSource.LoadForCriteria("Name='Customer'")[0];
Group _Group = GroupDataSource.LoadForCriteria("Name='Customer'");
Cannot implicitly convert type 'CommerceBuilder.Users.GroupCollection' to 'CommerceBuilder.Users.Group'
But this did work:
Group _Group = GroupDataSource.LoadForCriteria("Name='Customer'")[0];
Re: Getting group id of a group?
Sigh I'm having one of those weeks, sorry.
LoadForCriteria returns a collection of groups, not a single group. Thus the brackets narrow it down to the first object in the collection.
That's what I get for leaving my snug and cozy world of VB code
LoadForCriteria returns a collection of groups, not a single group. Thus the brackets narrow it down to the first object in the collection.
That's what I get for leaving my snug and cozy world of VB code

Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
Re: Getting group id of a group?
My 2 cents:
- an explicit reference to an element for a list of unknown size (or possible null value) is dangerous - get the object, test for existence, then use it.
- Avoid database calls when possible. Able pre-populates groups in the Token object, so you don't need a database call every time you want a reference to the Customers group
- an explicit reference to an element for a list of unknown size (or possible null value) is dangerous - get the object, test for existence, then use it.
- Avoid database calls when possible. Able pre-populates groups in the Token object, so you don't need a database call every time you want a reference to the Customers group
Code: Select all
CommerceBuilder.Users.Group group = Token.Instance.Store.Groups.Find(delegate(CommerceBuilder.Users.Group t) { return t.Name == "Customer"; });
if (group != null)
{
// do stuff here
}
Nick Cole
http://www.ethofy.com
http://www.ethofy.com
-
- Lieutenant (LT)
- Posts: 66
- Joined: Mon Jun 22, 2009 5:49 pm
Re: Getting group id of a group?
Thanks! Good advice.