Hi Marius!
I think you have misunderstood the use of choices on an attribute. A choice is a predefined alternative for the attribute, and will always be of the same type as the attribute, the choice will also have a name that is always of type String. With that said, it is also important to point out the value for an attribute is not limited to its choices.
I will use an example to illustrate. Say that you have an attribute named "Car Make" of type String. Now you may want to attach three choices to this attribute (these will also be of type String according to above):
When a user is supposed to input which car make he/she drives, you present a drop down list with the choices. In the case where the user selects "Other", maybe you want to show a free text field to input another car make. Say that the user inputs "Audi", then it will be OK to store the value "Audi" in the "Car Make" attribute, even though it is not among the choices.
So in you can use the choices in the manner you want to. As I see it you have two ways to solve your problem:
I hope this helps you to a solution!
//Tom
ps. It is worth mentioning that the administrative interface at the moment only handles choices of primitive types.
Ok, thanks for the enlightenment Tom! This gave me an opportunity to refactor some less good code, and I want to use the attribute choices in a dropdownlist, but the GetAttributes method returns nothing. My code looks like this:
DropDownList1.DataSource = user.GetAttributeValues<string>("UserListOfAttributeChoises");
DropDownList1.DataBind();
The attribute is UserListOfAttributes, an attribute attached to the IUser object, it is a list of string choices.
What am I missing?
Hi again,
The code that you have done actually gets all the values for a specific attribute. This method is used when you have an attribute that a user can enter several values for, for instance a multiselect listbox.
The method you are looking for to get hold of the choices is:
user.GetAttribute("yourAttributeName").Choices;
The "Choices" property will give you a AttributeValueChoiceCollection, where the "Text" property is the name of the choice.
//Tom
Ah, thanks a lot, this did clear things up a bit :)
Made myself a little method that gets all my choices as a list of strings:
public static List GetAttributes(StarCommunity.Core.Modules.Security.IUser user, string attributeName) { List list = new List(); IAttribute attr = CurrentUser.GetAttribute(attributeName); foreach (AttributeValueChoice choice in attr.Choices) { list.Add(choice.Text); } return list; }
I'm using various attributes for my IUser object to record extra information for each user. And I need to set a boolean value for each attribute, as to decide at runtime wether this attribute shoud be displayed or not. And I do not understand the point of Choice on the attributes, and the programmers guide does not mention this. What is Choice supposed to do for an attribute? Can I make for example a string attribute and define several strings as "valid" choices? And how do I access these choices?
I was kind of hoping that I could have mye attribute sting, and an additional choice attached to this attribute, in stead of making a string and bool attribute for each attribute value I need. example: if(user.GetAttributeValue<string>("attribute").GetChoice<bool>("attribute_choice")... Kind of thing
Any help, examples etc are most welcome!