CRM Developer Tip o’ the Day: Working with PartyLists

I ran into an issue in a plugin this week where I needed to decode the To and From fields of an Email. While this doesn’t seem like a huge task, these fields are of the type PartyList, which can be somewhat difficult to work with, at times.

I found the following response by Thomas Dekiere on StackExchange that allowed me to extract the list of recipients (I have made some modifications to his code):

Entity entity = (Entity)context.InputParameters["Target"];

stringsubject = entity.GetAttributeValue<String>("subject");
EntityCollection toCollection = entity.GetAttributeValue<EntityCollection>("to");

for(int i = 0; i < toCollection.Entities.Count; i++)
    ActivityParty ap = toCollection[i].ToEntity<ActivityParty>();

    EntityReference party = ap.PartyId;

    // do something with the Entity Reference

Here is how you create a PartyList field:

var entity = new Email {
        From = new[]
                new ActivityParty {
                        ParticipationTypeMask = new OptionSetValue(1), // Sender
PartyId = user
        To = new[]
                new ActivityParty
                        ParticipationTypeMask = new OptionSetValue(2), // ToRecipient
PartyId = contact
        Subject = "test email: " + DateTime.Now.ToLongDateString(),
        RegardingObjectId = contact

Hope that helps.

Leave a Reply 1 comment