Answered by:
How to Retrieve and Set partylist value form email activity using plugin

Question
-
Hi,
I'm trying to get value in the "From " field and store it in "Regarding" filed of the email activity. i'm doing this because incoming email is converted as a case through workflow, Customer filed in the case will take only Regarding field value from the email. Following is what i'm trying..
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
Entity entity = (Entity)context.InputParameters["Target"];
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Obtain the target business entity from the input parameters.
entity = (Entity)context.InputParameters["Target"];
try
{
// Verify that the entity represents a contact.
if (entity.LogicalName != "email") { return; }
ColumnSet col = new ColumnSet("to", "from", "regardingobjectid", "subject", "description");
entity = service.Retrieve(entity.LogicalName, entity.Id, col);
//Try 1string ent = ((EntityReference)((EntityCollection)entity["to"]).Entities[0].Attributes["partyid"]).LogicalName;
// Guid id = ((EntityReference)((EntityCollection)entity["to"]).Entities[0].Attributes["partyid"]).Id;
//Try 2
Entity contact = service.Retrieve("contact", ((EntityReference)((EntityCollection)entity["from"]).Entities[0].Attributes["partyid"]).Id, new ColumnSet(true));
....... .above are my two ways i'm trying to get the value but it's not wroking though, please let me know how to pass the value form "From field " to "regarding" field
I'm using " MS CRM 2011 on premise version.
Monday, April 22, 2013 5:10 PM
Answers
-
Try this instead. The code I originally posted was trying to set the 'activityparty' entity into the regarding (oops!), this is now correctly getting the entity reference from the activity party before setting the regarding. I also testing this and it works.
EntityCollection fromCollection = (EntityCollection)entity["from"]; if (fromCollection != null && fromCollection.Entities.Count > 0) { Entity fromParty = fromCollection[0]; //activityparty entity EntityReference fromRef = (EntityReference)fromParty["partyid"]; //actual entity ref entity["regardingobjectid"] = fromRef; }
Hope that helps
Paul
If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".
- Proposed as answer by Payman BiukaghazadehEditor Tuesday, April 23, 2013 3:26 PM
- Marked as answer by Pradeep47 Friday, April 26, 2013 4:56 AM
Tuesday, April 23, 2013 10:49 AM
All replies
-
Get it like this EntityCollection Recipients = email.GetAttributeValue<EntityCollection>("to"); foreach (var party in Recipients.Entities) { var partyName = party.GetAttributeValue<EntityReference>("partyid").Name; var partyId = party.GetAttributeValue<EntityReference>("partyid").Id; … }
Carsten Groth http://carstengroth.wordpress.com Microsoft Dynamics Certified Technology Specialist, MVP für Microsoft Dynamics CRM
- Proposed as answer by Payman BiukaghazadehEditor Monday, April 22, 2013 8:58 PM
Monday, April 22, 2013 5:27 PM -
Something like this will get the first value from the 'From' field and set it into the 'Regarding' field:
EntityCollection fromCollection = (EntityCollection)entity["from"]; if (fromCollection != null && fromCollection.Entities.Count > 0) { EntityReference from = fromCollection[0].ToEntityReference(); entity["regardingobjectid"] = from; }
However remember the 'From' field supports multiple values (where an email address matches multiple records) - so this will only get the first record in the collection.
Hope that helps
Paul
If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".
- Proposed as answer by Payman BiukaghazadehEditor Monday, April 22, 2013 8:55 PM
Monday, April 22, 2013 7:56 PM -
Thanks i'm able to get the value of "From" filed but not able to sett it.. it's not taking the EntityReference value int to regarding filed. can some pls guide me how to set the value of party list.Tuesday, April 23, 2013 9:06 AM
-
For your regarding field you have to create new ActivityParties with other participationtypemask.
Take a look at http://msdn.microsoft.com/en-us/library/gg328549.aspx for all the values.
So you cannot set it 1:1 - you first have to create a new ActivityParty for each value of your From field.
Carsten Groth http://carstengroth.wordpress.com Microsoft Dynamics Certified Technology Specialist, MVP für Microsoft Dynamics CRM
Tuesday, April 23, 2013 9:08 AM -
Try this instead. The code I originally posted was trying to set the 'activityparty' entity into the regarding (oops!), this is now correctly getting the entity reference from the activity party before setting the regarding. I also testing this and it works.
EntityCollection fromCollection = (EntityCollection)entity["from"]; if (fromCollection != null && fromCollection.Entities.Count > 0) { Entity fromParty = fromCollection[0]; //activityparty entity EntityReference fromRef = (EntityReference)fromParty["partyid"]; //actual entity ref entity["regardingobjectid"] = fromRef; }
Hope that helps
Paul
If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".
- Proposed as answer by Payman BiukaghazadehEditor Tuesday, April 23, 2013 3:26 PM
- Marked as answer by Pradeep47 Friday, April 26, 2013 4:56 AM
Tuesday, April 23, 2013 10:49 AM -
Thanks Paul, this code works like a charm !!!Friday, April 26, 2013 4:56 AM