QueryExpression Generic Function to Get GUID of a Specific Record – MS CRM 2011 C# Code

QueryExpression Generic Function to Get GUID of a Specific Record

 

private Guid GetRecordGuid(string EntityName , ColumnSet Colset, string AttributeName , string ConditionAttributeEquallTo ,string guidAttributeLogicalName)
{
Guid RecordId = Guid.Empty;
try
{
QueryExpression qe = new QueryExpression();
qe.EntityName = EntityName;
qe.ColumnSet = Colset;
ConditionExpression cond = new ConditionExpression(AttributeName, ConditionOperator.Equal, ConditionAttributeEquallTo);
FilterExpression fl = new FilterExpression();
fl.Conditions.Add(cond);
qe.Criteria.AddFilter(fl);
EntityCollection SearchContactResult = service.RetrieveMultiple(qe);
if (SearchContactResult.Entities.Count == 1)
{
foreach (Entity result in SearchContactResult.Entities)
{
RecordId = result.GetAttributeValue<Guid>(guidAttributeLogicalName);
}
}
}
catch (Exception ex)
{
lblContactSetResults.Text = ex.ToString();
}

return RecordId;
}

Cheers,

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s