Querying the values from a Picklist - dynamics-crm

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

Related

Select multiple column in Linq using List

I have a DropDownList and I fill it using linq. The code example below is working.
ddlPortal.DataSource = from rows in db.Portals select new
{rows.Id, rows.PortalName};
But I need to use it with List variable. What's the problem about the code below?
ddlPortal.DataSource = new List<string>(from rows in db.Portals select new {rows.Id.ToString(), rows.PortalName});
By the way I need to retrieve two columns for DataValueField and DataTextField of DropDownList .
That's not a List<string> but a list of an anonymous type. Use var:
var dataSource = db.Portals
.Select(rows => new {Id = rows.Id.ToString(),Portal = rows.PortalName} )
.ToList();
ddlPortal.DataSource = dataSource;

LINQ Query returning IQueryable, need it to be a single result

I currently have a LINQ query written in one my controllers that I want to return a single blog post (based off a model) with corresponding comments and topics.
This is what I currently have as my query which I used to return a list of all my blog posts for the home page. I added "where p.id == id (which is the parameter taken in by the ActionResult to fetch the correct post.
var post = from p in db.Set<BlogPost>()
where p.id == id
select new PostViewModel
{
Id = p.id,
Title = p.Title,
DateCreated = p.DateCreated,
Content = p.Content,
Topics = p.Topics,
Comments = p.Comments,
CommentCount = p.Comments.Count
};
return View(post);
The return currently is sending an IQueryable when I just want it to be a single post. Currently I have a foreach in my razor view which is useless and wrong but it works. How can I change this to do what I want?
You can do:
return View(post.SingleOrDefault());
Or if you want to have an exception in the case list is empty:
return View(post.Single());
Just add First() or Single() (which one is right for you depends on context) to your query:
return View(post.First());
It sound like you want to use First or Single:
return View(post.Single());
The difference between them is that Single will throw an exception if more than one matching row is found.
Just do post.First() that should do the trick. Really any function that produces a concrete value will work. First, FirstOrDefault, Single, SingleOrDefault, ToList, or ToArray
I have included the links to each method so you can see what works for you. It sounds like you will want a First or Single variation, depending on if you want errors if more than one post is pulled
replace your LINQ with this
var post = (from p in db.Set<BlogPost>()
where p.id == id
select new PostViewModel
{
Id = p.id,
Title = p.Title,
DateCreated = p.DateCreated,
Content = p.Content,
Topics = p.Topics,
Comments = p.Comments,
CommentCount = p.Comments.Count
}).FirstOrDefault();

Query the Description Value of a Picklist

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.

jqGrid display enum as string

I'm attempting to use the ASP.Net MVC version of jqGrid to display a simple data grid. One of the columns in my grid is an Enum and jqGrid is displaying is as an int whereas I want to display it as a string. How can I get jqGrid to display it as a string?
new JQGridColumn { DataField = "ApprovalStatus",
DataType = typeof(ApplicationStatusTypes),
Editable = false,
Width = 200},
public enum ApplicationStatusTypes
{
Unassessed = 0,
AssessmentInProgress = 1,
RequirementsNotMet = 2,
RequirementsPartiallyMet = 3,
RequirementsMet = 4,
Approved = 5
}
When the jqGrid is rendered the ApprovalStatus column shows up as an int instead of a string. I've tried messing around with DataFormatString on the column but to no avail.
I see that this is an old question, but for any lost soul, that will come here.
First step is to set a SetFormatter(Formatters.Select) for the column used for displaying an enum.
But then you need to provide a list of enum mappings. jqGrid expects them to be provided as a string in format of enumValue1:enumName1;enumValue2:enumName2 directly to .SetEditOptions(new EditOptions { Value = ... }) - unfortunately the API naming convention is broken here.
The string generation itself is pretty straightforward and can be generalized to the following expression:
string.Join(";", Enum.GetNames(typeof(T)).Zip(Enum.GetValues(typeof(T)).Cast<int>(), (text, val) => val.ToString() + ":" + text));
, where T is generic parameter being an enum type.

Can't get UpdateAttributeRequest to work

I am trying to use the Metadata Web Service of Dynamics CRM to update the order of some picklist options using the UpdateAttributeRequest message. Though I get to change the attribute's DisplayName using the MergeLabels = false option, the picklist option values themselves seem immutable.
Have you tried the Order Option Request?
// Create the request.
OrderOptionRequest orderOptionRequest = new OrderOptionRequest();
// Set the properties for the request
orderOptionRequest.AttributeLogicalName = "address1_addresstypecode";
orderOptionRequest.EntityLogicalName = EntityName.contact.ToString();
// Set the order for the options.
orderOptionRequest.Values = new int[] { 4, 3, 2, 1 };
// Execute the request
OrderOptionResponse orderOptionResponse = (OrderOptionResponse)metadataService.Execute(orderOptionRequest);

Resources