CRM 2011 – Metadaten auslesen (z.B. von Picklist)

CRM hat die Angewohnheit, dass es nicht den selektierten Wert (z.B. in einer Picklist) speichert, sondern den dahinterliegenden (internen) Code. Das mag durchaus Sinn machen, erschwert einem aber die Arbeit, wenn man programmatisch etwas bearbeiten möchte

Nachfolgend eine Funktion, welche die Metadaten entsprechend umwandelt, also z.B. selektierter Text in die interne Nummer.

[sourcecode language=“csharp“]
/// <summary>
/// Gets the metadata value.
/// </summary>
/// <param name="organization">The organization.</param>
/// <param name="entity">The entity.</param>
/// <param name="attribute">The attribute.</param>
/// <returns></returns>
public static string GetMetadataValue(contracts.IOrganization organization,Entity entity, string attribute)
{

IOrganizationService _service = OrgService(organization);
RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest
{
EntityLogicalName = entity.LogicalName,
LogicalName = attribute,
RetrieveAsIfPublished = true
};

RetrieveAttributeResponse attMetadata = (RetrieveAttributeResponse)_service.Execute(attributeRequest);
object oAttribute = entity[attribute];

string sReturn = string.Empty;
if (oAttribute.GetType().Equals(typeof(Microsoft.Xrm.Sdk.OptionSetValue)))
{

OptionMetadata[] optionList = null;
// Access the retrieved attribute.
//’Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata‘
if (attMetadata.AttributeMetadata.GetType().FullName.Contains("PicklistAttributeMetadata"))
{
PicklistAttributeMetadata retrievedPicklistAttributeMetadata =
(PicklistAttributeMetadata)attMetadata.AttributeMetadata;
// Get the current options list for the retrieved attribute.
optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
}
else if (attMetadata.AttributeMetadata.GetType().FullName.Contains("StatusAttributeMetadata"))
{
StatusAttributeMetadata retrievedPicklistAttributeMetadata =
(StatusAttributeMetadata)attMetadata.AttributeMetadata;
// Get the current options list for the retrieved attribute.
optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
}
else if (attMetadata.AttributeMetadata.GetType().FullName.Contains("StateAttributeMetadata"))
{
StateAttributeMetadata retrievedPicklistAttributeMetadata =
(StateAttributeMetadata)attMetadata.AttributeMetadata;
// Get the current options list for the retrieved attribute.
optionList = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();
}
else
return string.Empty;
// get the text values
int i = int.Parse((oAttribute as Microsoft.Xrm.Sdk.OptionSetValue).Value.ToString());
for (int c = 0; c < optionList.Length; c++)
{
OptionMetadata opmetadata = (OptionMetadata)optionList.GetValue(c);
if (opmetadata.Value == i)
{
sReturn = opmetadata.Label.UserLocalizedLabel.Label;
break;
}
}

}
else if (oAttribute.GetType().Equals(typeof(Microsoft.Xrm.Sdk.Money)))
{
sReturn = (oAttribute as Microsoft.Xrm.Sdk.Money).Value.ToString();
}
else if (oAttribute.GetType().Equals(typeof(Microsoft.Xrm.Sdk.EntityReference)))
{
sReturn = (oAttribute as Microsoft.Xrm.Sdk.EntityReference).Name;
}
else
{
sReturn = oAttribute.ToString();
}
if (sReturn == null || sReturn.Length == 0)
sReturn = "No Value";
return sReturn;
}

[/sourcecode]

Ich hoffe, ich konnte euch damit helfen 🙂

Cheers,
Chris

Leave a reply