I'm trying to get all entity's fields schema names without query to existing record. I haven't got any idea.
Also, How can I get it with QueryExpression, cause it retrieve all fields, which contain some info?
Andrii, I'm trying to do something like this
var query = new QueryExpression {EntityName = "", ColumnSet = new ColumnSet(true)};
var retrieve = service.RetrieveMultiple(query);
If you have the CRM SDK (http://www.microsoft.com/en-us/download/details.aspx?id=24004), you will find a utility called "Crmsvcutil.exe" in the SDK\Bin folder. This executable will generate classes that represent each of your CRM entities and their fields. You can then use the type.GetFields() method to derive the fields of these classes within your code. Once you have created these classes with the crmsvcutil, you can use LINQ queries to query your CRM data based on any criteria that suits your needs, including non null fields. The issue you will encounter with trying to use QueryExpression is that you must define the fields manually as shown below:
QueryExpression query = new QueryExpression("contact");
query.ColumnSet.AddColumns("firstname", "lastname");
query.Criteria.AddFilter(filter1);
I was looking for something similar and ended up cooking a useful method as such:
/// <summary>
/// Retrieves an entity's attributes.
/// </summary>
/// <param name="entityName">entity's name</param>
/// <param name="languageId">return display names of such language code</param>
/// <param name="xrm">xrmservicecontext object</param>
/// <returns>Dictionary<string, string></returns>
public static Dictionary<string, string> GetAttributes(string entityName, int languageId, XrmServiceContext xrm)
{
Dictionary<string, string> attributesData = new Dictionary<string, string>();
RetrieveEntityRequest metaDataRequest = new RetrieveEntityRequest();
RetrieveEntityResponse metaDataResponse = new RetrieveEntityResponse();
metaDataRequest.EntityFilters = EntityFilters.Attributes;
metaDataRequest.LogicalName = entityName;
metaDataResponse = (RetrieveEntityResponse)xrm.Execute(metaDataRequest);
foreach (AttributeMetadata a in metaDataResponse.EntityMetadata.Attributes)
{
//if more than one label for said attribute, get the one matching the languade code we want...
if (a.DisplayName.LocalizedLabels.Count() > 1)
attributesData.Add(a.LogicalName, a.DisplayName.LocalizedLabels.Where(x=>x.LanguageCode == languageId).SingleOrDefault().Label);
//else, get the only one available regardless of languade code...
if (a.DisplayName.LocalizedLabels.Count() == 1)
attributesData.Add(a.LogicalName, a.DisplayName.LocalizedLabels[0].Label);
}
return attributesData;
}
You could of course modify it to return whatever information you need about the entity's attributes, but I in my case I was looking for a way to replace the logical name of attributes that the CRM SDK uses to auto generate gridview column's with the actual display name of attributes.
You should try to use RetrieveEntity message to get all entity fields. You would not be able to get field in case it is equals to null. You will have to analyze - if attribute inside collection - it contains value, otherwise it is equals to null.