This blog you may use as a quick guide for accessing different type of attributes value from the link-entity using C#. So here in this blog I am retrieving data from the contact entity information with that I also retrieve link entity which is account entity where I am basically retrieving all type of attributes. I went through many blogs but seems there is no blog available where I could get all type attribute access code. I almost cover all the type of attributes which we usually retrieve in our day to the day development.
The tricky things which we usually miss alias name in the fetchXML. Alias name comes automatically when you build FetchXML using advanced find. You can give any name which you like in the fetchXML.
We need to use the alias name and dot before use the attributes name when you wanted to retrieve data using link entity. For example, if we retrieve accountnumber attributes value from account entity we need to use ab.accountnumber.
Here is the sample code you could refer -
var fethXML = @"
";
EntityCollection entityCollectionResult = new EntityCollection();
entityCollectionResult = Service.RetrieveMultiple(new FetchExpression(fethXML));
if (entityCollectionResult.Entities.Count > 0)
{
foreach (Entity entity in entityCollectionResult.Entities)
{
//---------Getting accountnumber (AccountNumber) - Single Line of Text Field--------//
string accountnumber;
AliasedValue accountnumberAliasVal = entity.GetAttributeValue
("ab.accountnumber");
if (accountnumberAliasVal != null)
{
accountnumber = accountnumberAliasVal.Value.ToString();
}
//---------revenue (Revenue) - Currency Field--------//
decimal revenue;
AliasedValue revenueAliasVal = entity.GetAttributeValue
("ab.revenue");
if (revenueAliasVal != null)
{
revenue = ((revenueAliasVal.Value) as Money).Value;
}
//---------createdon (CreatedOn) - Date and Time Field--------//
DateTime? createdon;
AliasedValue createdonAliasVal = entity.GetAttributeValue
("ab.createdon");
if (createdonAliasVal != null)
{
createdon = (DateTime)(createdonAliasVal.Value);
}
//---------exchangerate (ExchangeRate) - Decimal Number Field--------//
decimal exchangerate;
AliasedValue exchangerateAliasVal = entity.GetAttributeValue
("ab.exchangerate");
if (exchangerateAliasVal != null)
{
exchangerate = Convert.ToDecimal(exchangerateAliasVal.Value);
}
//---------address1_latitude (Address1_Latitude) - Floating Point Number Field--------//
decimal latitude;
AliasedValue latitudeAliasVal = entity.GetAttributeValue
("ab.address1_latitude");
if (latitudeAliasVal != null)
{
latitude = Convert.ToDecimal(latitudeAliasVal.Value);
}
//---------createdby (CreatedBy) - Lookup Field--------//
Guid createdByGuid = new Guid();
string createdByName = string.Empty;
AliasedValue createdByAliasVal = entity.GetAttributeValue
("ab.createdby");
if (createdByAliasVal != null)
{
createdByGuid = ((createdByAliasVal.Value) as EntityReference).Id;
createdByName = ((createdByAliasVal.Value) as EntityReference).Name;
}
//---------description (Description) - Multiple Lines of Text Field--------//
string description;
AliasedValue multilineTextAliasVal = entity.GetAttributeValue
("ab.description");
if (multilineTextAliasVal != null)
{
description = multilineTextAliasVal.Value.ToString(); ;
}
//---------accountcategorycode (Category) - Optionset Field--------//
Int32 categoryVal;
AliasedValue categoryAliasVal = entity.GetAttributeValue
("ab.accountcategorycode");
if (categoryAliasVal != null)
{
categoryVal = ((categoryAliasVal.Value) as OptionSetValue).Value;
}
//---------ownerid (OwnerId)-Owner type Field--------//
Guid ownerId = new Guid();
string ownerName = string.Empty;
AliasedValue ownerAliasVal = entity.GetAttributeValue
("ab.ownerid");
if (ownerAliasVal != null)
{
ownerId = ((ownerAliasVal.Value) as EntityReference).Id;
ownerName = ((ownerAliasVal.Value) as EntityReference).Name;
}
//---------donotemail - DoNotEMail--Two Options Field--------//
bool donotemailFlag;
AliasedValue donotemailAliasVal = entity.GetAttributeValue
("ab.donotemail");
if (donotemailAliasVal != null)
{
donotemailFlag = (Boolean)donotemailAliasVal.Value;
}
//---------onholdtime - OnHoldTime - Whole Number Field--------//
Int32 onholdtime;
AliasedValue onholdtimeAliasVal = entity.GetAttributeValue
("ab.onholdtime");
if (onholdtimeAliasVal != null)
{
onholdtime = (Int32)onholdtimeAliasVal.Value;
}
//---------Getting tel_hobby(Hobby) - Multiselect optionset--------//
Int32 hobby;
AliasedValue hobbyAliasVal = entity.GetAttributeValue
("ab.tel_hobby");
if (hobbyAliasVal != null)
{
List
optionsValue = new List
();
OptionSetValueCollection hobbies = ((hobbyAliasVal.Value) as OptionSetValueCollection);
foreach (OptionSetValue opt in hobbies)
{
optionsValue.Add(opt.Value);
}
}
}
}