Getting group id of a group?

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
RickSilver
Lieutenant (LT)
Lieutenant (LT)
Posts: 66
Joined: Mon Jun 22, 2009 5:49 pm

Getting group id of a group?

Post by RickSilver » Mon Aug 10, 2009 8:22 pm

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

RickSilver
Lieutenant (LT)
Lieutenant (LT)
Posts: 66
Joined: Mon Jun 22, 2009 5:49 pm

Re: Getting group id of a group?

Post by RickSilver » Mon Aug 10, 2009 8:25 pm

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'

User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

Re: Getting group id of a group?

Post by AbleMods » Mon Aug 10, 2009 8:59 pm

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

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Getting group id of a group?

Post by mazhar » Tue Aug 11, 2009 4:10 am

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;
    	}

RickSilver
Lieutenant (LT)
Lieutenant (LT)
Posts: 66
Joined: Mon Jun 22, 2009 5:49 pm

Re: Getting group id of a group?

Post by RickSilver » Tue Aug 11, 2009 8:21 am

Thanks for the solutions!

RickSilver
Lieutenant (LT)
Lieutenant (LT)
Posts: 66
Joined: Mon Jun 22, 2009 5:49 pm

Re: Getting group id of a group?

Post by RickSilver » Tue Aug 11, 2009 8:28 am

AbleMods, that didn't work.

Error: 'CommerceBuilder.Users.GroupDataSource' does not contain a definition for 'LoadforCriteria'

User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

Re: Getting group id of a group?

Post by AbleMods » Tue Aug 11, 2009 8:42 am

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 :oops:
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

User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

Re: Getting group id of a group?

Post by AbleMods » Tue Aug 11, 2009 8:43 am

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

RickSilver
Lieutenant (LT)
Lieutenant (LT)
Posts: 66
Joined: Mon Jun 22, 2009 5:49 pm

Re: Getting group id of a group?

Post by RickSilver » Tue Aug 11, 2009 4:41 pm

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];

User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

Re: Getting group id of a group?

Post by AbleMods » Tue Aug 11, 2009 4:45 pm

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 :)
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

User avatar
nickc
Captain (CAPT)
Captain (CAPT)
Posts: 276
Joined: Thu Nov 29, 2007 3:48 pm

Re: Getting group id of a group?

Post by nickc » Tue Aug 11, 2009 5:08 pm

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
            }

RickSilver
Lieutenant (LT)
Lieutenant (LT)
Posts: 66
Joined: Mon Jun 22, 2009 5:49 pm

Re: Getting group id of a group?

Post by RickSilver » Tue Aug 11, 2009 5:32 pm

Thanks! Good advice.

Post Reply