Search code examples
c#pluginsdynamics-crmdynamics-crm-2011

How to get the Contact Guids from a PartyList in a Plugin?


I'm making a plugin that triggers on the create message of a custom activity SMS. These plugin will send the actual sms using a third party sms service provider.

Therefore i need to get the mobilephone numbers for every contact in the "To" field of the SMS activity. This is a field of type: PartyList.

I'm currently using the following code:

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

IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

Content = entity.GetAttributeValue<String>("subject");
Recipients = entity.GetAttributeValue<EntityCollection>("to");

for (int i = 0; i < Recipients.Entities.Count; i++)
{
  Entity ent= Recipients[i];

  string number = ent["MobilePhone"].ToString();    
}

But this is not working, i think the ent variable contains no attributes.

I've tried coding with ActivityParty also but not luck either.

I hope someone of you can help me with this one.

Thanks!


Solution

  • Here's is how I finally did it:

    EntityCollection Recipients;
    Entity entity = (Entity) context.InputParameters["Target"];
    
    IOrganizationServiceFactory serviceFactory 
      = (IOrganizationServiceFactory)serviceProvider.GetService(
        typeof(IOrganizationServiceFactory)); 
    IOrganizationService service = serviceFactory
      .CreateOrganizationService(context.UserId); 
    
    Content = entity.GetAttributeValue<String>("subject"); 
    Recipients = entity.GetAttributeValue<EntityCollection>("to"); 
    
    for (int i = 0; i < Recipients.Entities.Count; i++)
    {
      ActivityParty ap = Recipients[i].ToEntity<ActivityParty>();
      String contactid = ap.PartyId.Id.ToString();
      Contact c = (Contact) service.Retrieve(
        Contact.EntityLogicalName,
        ap.PartyId.Id,
        new ColumnSet(new string[]{ "mobilephone" }));
      String mobilephone = c.MobilePhone;
      ...
    }