3

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!

ccellar
  • 10,326
  • 2
  • 38
  • 56
ThdK
  • 9,916
  • 23
  • 74
  • 101

2 Answers2

7

Recipients is a list of ActivityParty, not of contacts, accounts, ... . Therefore you have to read its PartyId

EntityReference partyId = ent.GetAttributeValue<EntityReference>("partyid");

With this information you have to look for the record which is referecend with this partyID. It could be a contact, an account, a systemuser, ... You'll get this information trough

var partyType = partyId.LogicalName;

Then you could retrieve the record this record in order to read the number.

ccellar
  • 10,326
  • 2
  • 38
  • 56
  • Thanks for the reply. I've tried using ActivityParty. But it never worked either. What do i have to change in my code? 1. ActivityParty Recipients; (or ActivityParty[]) ? 2. Recipients = entity.GetAttributeValue("to"); (or ActivityParty[]) ? When i do this, i 'll get a error that i cannot cast EntityCollection into ActivityParty. When i use the entitycollection type, and cast each object in the collection to an activityparty i get error: "Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity' to type 'ActivityParty'." What am I still doing wrong? – ThdK Nov 17 '11 at 07:52
7

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;
  ...
} 
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
ThdK
  • 9,916
  • 23
  • 74
  • 101
  • I'm using early-bound types in my plugin and fetching an Email's To field was problematic in the same way. Changing the code to late-bound as shown here solved my issue as well. – Sal Sep 26 '14 at 13:45