Tuesday, March 26, 2013

CRM 2011: Read contact id from Email From field C#

I used this method to Get a contact id from email "From" field. This method can be customized and you can get any entity you need from this field.


 public Guid? GetContactIdInFromField(IEnumerable<ActivityParty> party)
        {
            var fromItems = party.Select(c => c.ToEntity<ActivityParty>()).ToArray();

            var fromContacts = from c in fromItems
                               where c.PartyId.LogicalName == Contact.EntityLogicalName
                               select c;
            var contactRef = fromContacts.FirstOrDefault();

            if (contactRef == null)
                return null;

            return contactRef.PartyId.Id;
        }

Call function:
var contactFromId = GetContactIdInFromField(emailEntity.From);

Hope this helps.

No comments:

Post a Comment