Thursday, May 7, 2009

Return column label from plugin

this function used to return label for field.
can be used for plugin,audit etc...

public static string GetColumnLabel(IPluginExecutionContext context, string entityTypeName, string columnName)
{
IMetadataService metadataService = context.CreateMetadataService(false);
Microsoft.Crm.SdkTypeProxy.Metadata.RetrieveAttributeRequest request = new Microsoft.Crm.SdkTypeProxy.Metadata.RetrieveAttributeRequest();
request.EntityLogicalName = entityTypeName;
request.LogicalName = columnName.ToLower();
Microsoft.Crm.SdkTypeProxy.Metadata.RetrieveAttributeResponse response = (Microsoft.Crm.SdkTypeProxy.Metadata.RetrieveAttributeResponse)metadataService.Execute(request);
AttributeMetadata am = response.AttributeMetadata;
if (am != null)
return am.DisplayName.UserLocLabel.Label;
return "";
}

Enjoy,
Rami Heleg

Retrieve Entity field value

This function used to return field value inside plugin,

public static object GetEntityFieldValue(IPluginExecutionContext context,string entityName, Guid instanceId,string fieldName){
ICrmService pservice = context.CreateCrmService(false);
TargetRetrieveDynamic targetRetrieve = new TargetRetrieveDynamic();
targetRetrieve.EntityName = entityName;
targetRetrieve.EntityId = instanceId;
RetrieveRequest retrieve = new RetrieveRequest();
retrieve.Target = targetRetrieve;
retrieve.ColumnSet = new AllColumns();
retrieve.ReturnDynamicEntities = true;
RetrieveResponse retrieved = (RetrieveResponse)pservice.Execute(retrieve);
DynamicEntity entity = (DynamicEntity)retrieved.BusinessEntity;
return entity.Properties[fieldName];
}

Enjoy,
Rami Heleg