CRM 2011 Activity Party Add, Remove, Delete from code
Sometimes we want to append the activity party list, update particular item in the existing activity party list, or remove specific activity party from the list.
Below is what I discovered on how to do this. Since we can't use the Create/Update/Delete method from the service context on ActivityParty entity, we need to alter the content of the list before attaching to the Activity Field.
In my case I have an ActivityRecipient custom entity that have N:1 to Email entity. Whenever the user add/update/delete this custom entity, the corresponding 'To' recipients gets updated as well.
I use this function in my pre create/update/delete message of my ActivityRecipient entity.
Note: for some reason Recurring Appointment doesn't like Organization Service Context (early bound), therefore we have to use IOrganizationService with Retrieve Request.
Hope this helps,
Andreas
Below is what I discovered on how to do this. Since we can't use the Create/Update/Delete method from the service context on ActivityParty entity, we need to alter the content of the list before attaching to the Activity Field.
In my case I have an ActivityRecipient custom entity that have N:1 to Email entity. Whenever the user add/update/delete this custom entity, the corresponding 'To' recipients gets updated as well.
I use this function in my pre create/update/delete message of my ActivityRecipient entity.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | /// <summary> /// Populate related activity parties /// </summary> private static void PopulateParties(IOrganizationService service, OrgServiceContext serviceContext, new_activityrecipient activityRecipient, string message) { if (message == "create" ) { //Email if (activityRecipient.new_emailid != null ) { var email = serviceContext.EmailSet.Where(e => e.ActivityId == activityRecipient.new_emailid.Id).SingleOrDefault(); ActivityParty toParty = new ActivityParty { PartyId = activityRecipient.new_contactid }; email.To = email.To.Concat( new ActivityParty[] { toParty }); serviceContext.UpdateObject(email); serviceContext.SaveChangesWithErrorCheck(); } //Appointment if (activityRecipient.new_appointmentid != null ) { var appointment = serviceContext.AppointmentSet.Where(a => a.ActivityId == activityRecipient.new_appointmentid.Id).SingleOrDefault(); ActivityParty reqAttendees = new ActivityParty { PartyId = activityRecipient.new_contactid }; appointment.RequiredAttendees = appointment.RequiredAttendees.Concat( new ActivityParty[] { reqAttendees }); serviceContext.UpdateObject(appointment); serviceContext.SaveChangesWithErrorCheck(); } //Recurring Appointment if (activityRecipient.new_recurringappoinmentid != null ) { RecurringAppointmentMaster appointment = (RecurringAppointmentMaster)service.Retrieve(RecurringAppointmentMaster.EntityLogicalName, activityRecipient.new_recurringappoinmentid.Id, new ColumnSet( "subject" , "requiredattendees" )); ActivityParty reqAttendees = new ActivityParty { PartyId = activityRecipient.new_contactid }; appointment.RequiredAttendees = appointment.RequiredAttendees.Concat( new ActivityParty[] { reqAttendees }); service.Update(appointment); } } if (message == "update" ) { var retrievedActivityRecipient = serviceContext.new_activityrecipientSet.Where(x => x.Id == activityRecipient.Id).SingleOrDefault(); //Email if (retrievedActivityRecipient.new_emailid != null ) { var email = serviceContext.EmailSet.Where(e => e.ActivityId == retrievedActivityRecipient.new_emailid.Id).SingleOrDefault(); ActivityParty toParty = serviceContext.ActivityPartySet.Where(ap => ap.ActivityId.Id == email.ActivityId && ap.PartyId == retrievedActivityRecipient.new_contactid).SingleOrDefault(); ActivityParty toNewParty = new ActivityParty { PartyId = activityRecipient.new_contactid }; List<ActivityParty> recipients = email.To.ToList(); int oldIndex = recipients.FindIndex(ap => ap.ActivityPartyId == toParty.ActivityPartyId); recipients[oldIndex] = toNewParty; email.To = recipients; serviceContext.UpdateObject(email); serviceContext.SaveChangesWithErrorCheck(); } //Appoinment if (retrievedActivityRecipient.new_appointmentid != null ) { var appointment = serviceContext.AppointmentSet.Where(a => a.ActivityId == retrievedActivityRecipient.new_appointmentid.Id).SingleOrDefault(); ActivityParty reqAttendees = serviceContext.ActivityPartySet.Where(ap => ap.ActivityId.Id == appointment.ActivityId && ap.PartyId == retrievedActivityRecipient.new_contactid).SingleOrDefault(); ActivityParty reqNewAttendees = new ActivityParty { PartyId = activityRecipient.new_contactid }; List<ActivityParty> recipients = appointment.RequiredAttendees.ToList(); int oldIndex = recipients.FindIndex(ap => ap.ActivityPartyId == reqAttendees.ActivityPartyId); recipients[oldIndex] = reqNewAttendees; appointment.RequiredAttendees = recipients; serviceContext.UpdateObject(appointment); serviceContext.SaveChangesWithErrorCheck(); } //Recurring Appoinment if (retrievedActivityRecipient.new_recurringappoinmentid != null ) { RecurringAppointmentMaster appointment = (RecurringAppointmentMaster)service.Retrieve(RecurringAppointmentMaster.EntityLogicalName, retrievedActivityRecipient.new_recurringappoinmentid.Id, new ColumnSet( "subject" , "requiredattendees" )); ActivityParty reqAttendees = serviceContext.ActivityPartySet.Where(ap => ap.ActivityId.Id == appointment.ActivityId && ap.PartyId == retrievedActivityRecipient.new_contactid).SingleOrDefault(); ActivityParty reqNewAttendees = new ActivityParty { PartyId = activityRecipient.new_contactid }; List<ActivityParty> recipients = appointment.RequiredAttendees.ToList(); int oldIndex = recipients.FindIndex(ap => ap.ActivityPartyId == reqAttendees.ActivityPartyId); recipients[oldIndex] = reqNewAttendees; appointment.RequiredAttendees = recipients; service.Update(appointment); } } if (message == "delete" ) { //Email if (activityRecipient.new_emailid != null ) { var email = serviceContext.EmailSet.Where(e => e.ActivityId == activityRecipient.new_emailid.Id).SingleOrDefault(); ActivityParty toParty = serviceContext.ActivityPartySet.Where(ap => ap.ActivityId.Id == email.ActivityId && ap.PartyId == activityRecipient.new_contactid).SingleOrDefault(); List<ActivityParty> recipients = email.To.ToList(); email.To = recipients.Where(ap => ap.ActivityPartyId != toParty.ActivityPartyId); serviceContext.UpdateObject(email); serviceContext.SaveChangesWithErrorCheck(); } //Appointment if (activityRecipient.new_appointmentid != null ) { var appointment = serviceContext.AppointmentSet.Where(a => a.ActivityId == activityRecipient.new_appointmentid.Id).SingleOrDefault(); ActivityParty reqAttendees = serviceContext.ActivityPartySet.Where(ap => ap.ActivityId.Id == appointment.ActivityId && ap.PartyId == activityRecipient.new_contactid).SingleOrDefault(); List<ActivityParty> recipients = appointment.RequiredAttendees.ToList(); appointment.RequiredAttendees = recipients.Where(ap => ap.ActivityPartyId != reqAttendees.ActivityPartyId); serviceContext.UpdateObject(appointment); serviceContext.SaveChangesWithErrorCheck(); } //Recurring Appointment if (activityRecipient.new_recurringappoinmentid != null ) { RecurringAppointmentMaster appointment = (RecurringAppointmentMaster)service.Retrieve(RecurringAppointmentMaster.EntityLogicalName, activityRecipient.new_recurringappoinmentid.Id, new ColumnSet( "subject" , "requiredattendees" )); ActivityParty reqAttendees = serviceContext.ActivityPartySet.Where(ap => ap.ActivityId.Id == appointment.ActivityId && ap.PartyId == activityRecipient.new_contactid).SingleOrDefault(); List<ActivityParty> recipients = appointment.RequiredAttendees.ToList(); appointment.RequiredAttendees = recipients.Where(ap => ap.ActivityPartyId != reqAttendees.ActivityPartyId); service.Update(appointment); } } } |
Note: for some reason Recurring Appointment doesn't like Organization Service Context (early bound), therefore we have to use IOrganizationService with Retrieve Request.
Hope this helps,
Andreas
Comments
Post a Comment