Query the Description Value of a Picklist - dynamics-crm

I am trying to get the value from the Description field of a picklist in CRM, this is what I am using to get the Label value, how would I change it to get the Description Value?
RetrieveAttributeRequest request = new RetrieveAttributeRequest();
request.EntityLogicalName = "opportunity";
request.LogicalName = "country";
RetrieveAttributeResponse response = (RetrieveAttributeResponse)orgService.Execute(request);
PicklistAttributeMetadata picklist = (PicklistAttributeMetadata)response.AttributeMetadata;
foreach (OptionMetadata option in picklist.OptionSet.Options)
{
string picklistlabel = option.Label.UserLocalizedLabel.Label.ToString();
if (p.Column_16.ToString().ToUpper() == picklistlabel.ToString().ToUpper())
{
countryid= option.Value;
}
}
Thanks!

You can find the description for a specific option in a optionset by accessing the Description property.
Like this:
string description = option.Description.UserLocalizedLabel.Label.ToString();
Here is a list of members exposed by the PicklistAttributeMetadata.

Related

How to get ItemIndex in Transferred event of Rad ListBox

I'm trying to get selected Item Text and value of RadListBox in Transferred event.
I tried following code:
protected void listbox_Initial_Transferred(object sender, RadListBoxTransferredEventArgs e)
{
string id = e.Items[0].Value.ToString();
string description = e.Items[0].Text.ToString();
}
here i passed "0" as index. Its working fine.
Now i got stuck here to fetch the selected index of Item.
Looking forward for your comments and replies.
Thanks.
Please try with the below code snippet.
// for single item
string id = RadListBox1.SelectedItem.Value.ToString();
string description = RadListBox1.SelectedItem.Text.ToString();
// for multiple item
foreach (RadListBoxItem item in RadListBox1.SelectedItems)
{
string _id = item.Value.ToString();
string _description = item.Text.ToString();
}
AS per your sample code, please also try with the below code snippet.
// for multiple item
foreach (RadListBoxItem item in e.Items)
{
string _id = item.Value.ToString();
string _description = item.Text.ToString();
}

Linq replace null/empty value with another value

In sql server I create views where I can check for null values and replace them with a default if I want (i.e. ISNULL(PrimaryPhone, 'No Primary #') AS PrimaryPhone. I used a lot of views in my asp.net web forms application, but I am now learning MVC 4 and want to start out right.
I have a model, controller and view set up for my client table. I would like to be able to replace null/empty values with ("Not Entered") or something like that.
I believe this needs to go in the controller, please correct me if I'm wrong.
I saw an example that uses hasvalue, but it is not available through intellisense.
How would I replace empty/null values without using DefaultValue in my model?
Thanks
var result = db.Clients.Select(x => new
{
CustomerID = x.CustomerID,
UserID = x.UserID,
FullName = x.FullName,
EmailAdd = x.emailadd.DefaultIfEmpty("No Email"),....
You can use the ?? operator to set a default value when something is null:
var result = db.Clients.Select(x => new
{
CustomerID = x.CustomerID,
UserID = x.UserID,
FullName = x.FullName,
EmailAdd = x.emailadd ?? "No Email", ...
If you need more control, for example checking for both null and empty, you can also use the ?: operator (also known as the "conditional operator" or "ternary operator"):
var result = db.Clients.Select(x => new
{
CustomerID = x.CustomerID,
UserID = x.UserID,
FullName = x.FullName,
EmailAdd = string.IsNullOrEmpty(x.emailadd) ? "No Email" : x.emailadd, ...

Sitecore 6 WFFM: ListField value?

I am building a complex WFFM user control that extends BaseUserControl. This control has multiple fields that get prepopulated based on some business logic. One of the fields is supposed to be a drop down which shows values from a series of Sitecore items. Here is the definition of my ListField property:
private string myListField;
[VisualProperty("My List Field:", 100),
VisualCategory("Appearance"), VisualFieldType(typeof(ListField))]
public string MyListField{
get { return myListField; }
set { myListField= value; }
}
When I debug this, the content of titleFieldList is a string that contains the following XML in URL encoded format:
%3Cquery%20t%3D%22root%22%20vf%3D%22__ID%22%20tf%3D%22Value%22%3E%3Cvalue%3E%7B814FC177-2750-48D6-B7B7-4EE87012C637%7D%3C%2Fvalue%3E%3C%2Fquery%3E
which, decode, is:
<query t="root" vf="__ID" tf="Value">
<value>{814FC177-2750-48D6-B7B7-4EE87012C637}</value>
</query>
I understand the meaning of this XML. It says that all the children of the item whose ID is that Guid are supposed to be used to populate my list, using the template field "__ID" for the value and the template field "value" for the text.
Can someone help me understand what am I supposed to do to bind an asp:DropDownList to this? Is this a particular sitecore object that has been serialized and encoded?
Is there an sc:control that can handle this?
Thanks!
** EDIT **
So I tried the following piece of code
string encodedQuery = TitleFieldList;
string query = HttpUtility.UrlDecode(encodedQuery);
XDocument xmlQuery = XDocument.Parse(query);
if (xmlQuery.Element("query") != null)
{
Dictionary<string, string> nodesDictionary = new Dictionary<string, string>();
string root = xmlQuery.Element("query").Element("value").Value;
string value = xmlQuery.Element("query").Attribute("vf").Value;
string text = xmlQuery.Element("query").Attribute("tf").Value;
Item rootItem = SitecoreUtility.GetItemWithoutSecurity(new ID(root));
ChildList childList = rootItem.GetChildren();
foreach (Item child in childList)
{
string theValue = (value == "__ID") ? child.ID.ToString() : child.Fields[value].ToString();
string theText = child.Fields[text].ToString();
nodesDictionary.Add(theText, theValue);
}
titleDropDownList.DataSource = nodesDictionary;
titleDropDownList.DataTextField = "key";
titleDropDownList.DataValueField = "value";
titleDropDownList.DataBind();
}
and it works. The dropdownlist is populated with the correct data coming from the fields that were selected in the editor. I just can't believe that there is no easier way to do this. Plus how am I supposed to honor the MultipleSelectedValueField and the EmptyChoiceField if present?
Try to change the return type of your property and add attribute TypeConverter. The type specified in TypeConverter is responsible for converting raw string value to a property's return type.
ListItemCollectionConverter - is a converter provided by WFFM
[VisualProperty("My List Field:", 100)]
[VisualCategory("Appearance")]
[VisualFieldType(typeof(ListField))]
[TypeConverter(typeof(Sitecore.Form.Web.UI.Controls.ListItemCollectionConverter.ListItemCollectionConverter))]
public Sitecore.Form.Web.UI.Controls.ListItemCollection MyListField{
get { return myListField; }
set { myListField= value; }
}

Getting property value based on its column attribute value

List<MyModel1> myModel1 = new List<MyModel1>();
MyUserModel myUserModel = new MyUserModel();
List<MyModel2> myModel2 = new List<MyModel1>();
myModel1 = m_Service1.GetMyModelFields();
myUserModel = m_Service2.GetMyUserDetails();
myModel2 = (from myModel1Field in myModel1
select new MyModel2 { FieldCaption = myModel1Field.FieldAlias,
FieldValue = "" }).ToList<MyModel2>();
myModel1Field.FieldAlias text will be same as value of one of the Column attribute of one of the property in myUserModel. So I have to search for the column atribute(Name) in myUserModel and get the corresponding property values and assign it to 'FieldValue'. If I can't find the value in myUserModel I can set 'FieldValue' as "NA"
One way to get the column attribute(Name) value of for a property is as below when I know the Property name.
myUserModel.GetType().GetProperty("FirstName").GetCustomAttributes(typeof(System.Data.Linq.Mapping.ColumnAttribute), false).Cast<System.Data.Linq.Mapping.ColumnAttribute>().Single().Name
But in my case Property name will not be known. I have to find the property based on the myModel1Field.FieldAlias value. How to go about this. Please suggest.
MyUserModel with one of it's properties
public class MyUserModel {
[Column(Name = "first_name", DbType = "varchar")]
public string FirstName { get; set; }
}
Now if myModel1Field.FieldAlias is 'first_name' then I have to search in MyUserModel for a property with Column attribute(Name) as first_name. If it exists i have to set it's value to 'FieldValue'. Else set 'FieldValue' as "NA".
If you want to get the value of a property and you only know the Name property of one of the ColumnAttribute attributes on it what you can do is this:
// Let's say you have the user model like so:
MyUserModel myUserModel = new MyUserModel { FirstName = "A", LastName = "B"};
// And then you want the value of the property that has the Column attribute Name "first_name"
string searchName = "first_name";
// Using some lambda you can do this (I do not know how to do this in LINQ syntax, sorry)
object propertyValue = typeof (MyUserModel).GetProperties()
.Where(p =>
{
var attrib = (ColumnAttribute)p
.GetCustomAttributes(typeof (ColumnAttribute), false)
.SingleOrDefault();
return (attrib != null &&
attrib.Name.Equals(searchName));
})
.Select(p => p.GetValue(myUserModel, null))
.FirstOrDefault();
if(propertyValue != null)
{
// Do whatever you want with the string "A" here - I suggest casting it to string! :-)
}
Is that what you want?

Querying the values from a Picklist

I want to query the values from the different Picklist in Opportunity. I don't want to query the actual Opportunity, just the values from the Picklist. So if I have a Picklist called Source and it has the values of 1, 2, 3, 4, 5. I want to query the picklist and get back those values. Is this possible? If so, how in the world do you do it? Thanks!
Figured it out:
RetrieveAttributeRequest request = new RetrieveAttributeRequest();
request.EntityLogicalName = "opportunity";
request.LogicalName = "new_businessunit";
RetrieveAttributeResponse response = (RetrieveAttributeResponse)orgService.Execute(request);
PicklistAttributeMetadata picklist = (PicklistAttributeMetadata)response.AttributeMetadata;
int? businessid = null;
foreach (OptionMetadata option in picklist.OptionSet.Options)
{
string businesslabel = option.Label.UserLocalizedLabel.Label.ToString();
}

Resources