Page 1 of 1
Getting group id of a group?
Posted: Mon Aug 10, 2009 8:22 pm
by RickSilver
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
Re: Getting group id of a group?
Posted: Mon Aug 10, 2009 8:25 pm
by RickSilver
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'
Re: Getting group id of a group?
Posted: Mon Aug 10, 2009 8:59 pm
by AbleMods
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;
Re: Getting group id of a group?
Posted: Tue Aug 11, 2009 4:10 am
by mazhar
RickSilver 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'
The correct fix for your code would be
Code: Select all
GroupCollection storeGroups = GroupDataSource.LoadForStore();
for (int i=0; i<storeGroups.Count; i++)
{
Group group = storeGroups[i];
int groupId = groupd.GroupId;
}
Re: Getting group id of a group?
Posted: Tue Aug 11, 2009 8:21 am
by RickSilver
Thanks for the solutions!
Re: Getting group id of a group?
Posted: Tue Aug 11, 2009 8:28 am
by RickSilver
AbleMods, that didn't work.
Error: 'CommerceBuilder.Users.GroupDataSource' does not contain a definition for 'LoadforCriteria'
Re: Getting group id of a group?
Posted: Tue Aug 11, 2009 8:42 am
by AbleMods
RickSilver wrote:AbleMods, that didn't work.
Error: 'CommerceBuilder.Users.GroupDataSource' does not contain a definition for 'LoadforCriteria'
Whoops I forgot to capitalize the "f" in "for" ... should be ".LoadForCriteria"
My Bad

Re: Getting group id of a group?
Posted: Tue Aug 11, 2009 8:43 am
by AbleMods
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.
Re: Getting group id of a group?
Posted: Tue Aug 11, 2009 4:41 pm
by RickSilver
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];
Re: Getting group id of a group?
Posted: Tue Aug 11, 2009 4:45 pm
by AbleMods
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

Re: Getting group id of a group?
Posted: Tue Aug 11, 2009 5:08 pm
by nickc
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
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
}
Re: Getting group id of a group?
Posted: Tue Aug 11, 2009 5:32 pm
by RickSilver
Thanks! Good advice.