Answered by:
Retrieve attributes values from entity calling plugin

Question
-
Hi,
In a plugin code for CRM 2011, if I want to read attributes values from the entity which is calling the plugin, can I get them in this way?
string x = ((Entity)context.InputParameters["Target"]).Attributes["attributeName"].ToString();
or I have to use the Retrieve method?
What I want is to read the attributes of the entity which is calling the Plugin.
Is there a way to do that without having to use the Retrieve method?
- Edited by ms_crm Wednesday, November 23, 2011 10:41 AM More info
Wednesday, November 23, 2011 10:13 AM
Answers
-
Yes you can, you don't have to use retrieve, one exception is attribute collection will only contain the attributes which are modified by the user. When the user updating a record, you can register a pre-image with the update plugin registration process, and you don't need to call retieve and you can get attribute value from pre-entity.
please refer the following sample code
var context = (Microsoft.Xrm.Sdk.IPluginExecutionContext) serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext)); if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { var targetEntity = (Entity)context.InputParameters["Target"]; if (targetEntity.LogicalName != "account") return; //Retrieve data. var trackingOptionObj = targetEntity.GetAttributeValue<OptionSetValue>("new_trackingoption"); var accountId = targetEntity.GetAttributeValue<Guid>("accountid"); var newCustomerStatus = targetEntity.GetAttributeValue<OptionSetValue>("new_customerstatus").Value; var current_syn_customerid = targetEntity.GetAttributeValue<string>("new_customerid"); var current_syn_debtorclassid = targetEntity.GetAttributeValue<EntityReference>("new_customerclass").Id.ToString(); //Add attribute Guid contactId = Guid.Empty; //sepcify value targetEntity.Attributes.Add("primarycontactid", new EntityReference("contact", contactId)); }
to read pre or post image, please refer the following code, reading attribute value is same as above.var preEntity = (Entity)context.PreEntityImages[preimagename]; var postEntity = (Entity)context.PostEntityImages[postimagename];
Thomas T(MCBMSS) If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".- Edited by Thomas Thankachan Thursday, November 24, 2011 4:16 AM
- Proposed as answer by Thomas Thankachan Thursday, November 24, 2011 4:16 AM
- Marked as answer by DavidJennawayMVP, Moderator Thursday, January 12, 2012 1:50 PM
Thursday, November 24, 2011 4:00 AM
All replies
-
If the 'Target' property contains an Entity object, you can use the above code to retrieve the attributes. You don't need to call the retrieve method.
If the plugin is called by an update message, you might also want to look into using Pre and Post Images.
Kind regards
Kirsten
Wednesday, November 23, 2011 11:27 AM -
Hi
If you want to retrieve values from same entity then you can directly use them from plugin Context
If you want to access any other value you can user retrieve or retrieve multiple method of Organization service object from same plugin
Following is the sample example of plugins
public void Execute(IServiceProvider serviceProvider) { Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext)); Microsoft.Xrm.Sdk.IOrganizationServiceFactory serviceFactory = (Microsoft.Xrm.Sdk.IOrganizationServiceFactory)serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IOrganizationServiceFactory)); try { // The InputParameters collection contains all the data passed in the message request. if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { // Accress the Attributes from same entity // Accress the Attributes from same entity Entity entity = context.InputParameters["Target"] as Entity; if (entity != null) { if (!entity.Contains("yourAttributeName")) entity.Attributes.Add("yourAttributeName", "yourValue"); else entity["yourAttributeName"] = "your Value"; } } // Accress the Attributes from other entity // Accress the Attributes from other entity IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); var manyEntityies = service.RetrieveMultiple(new QueryExpression("yourOtherEntityName") { }); } catch (InvalidPluginExecutionException ex) { throw ex; } }
Many Thanks -Bhautik Desai xRM Technologies- Proposed as answer by Bhautik Desai-XRM Tech Wednesday, November 23, 2011 12:05 PM
Wednesday, November 23, 2011 12:05 PM -
Hi,
thank you for your reply.
We usually use the above code for a Create message?
Wednesday, November 23, 2011 12:18 PM -
Yes you can, you don't have to use retrieve, one exception is attribute collection will only contain the attributes which are modified by the user. When the user updating a record, you can register a pre-image with the update plugin registration process, and you don't need to call retieve and you can get attribute value from pre-entity.
please refer the following sample code
var context = (Microsoft.Xrm.Sdk.IPluginExecutionContext) serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext)); if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { var targetEntity = (Entity)context.InputParameters["Target"]; if (targetEntity.LogicalName != "account") return; //Retrieve data. var trackingOptionObj = targetEntity.GetAttributeValue<OptionSetValue>("new_trackingoption"); var accountId = targetEntity.GetAttributeValue<Guid>("accountid"); var newCustomerStatus = targetEntity.GetAttributeValue<OptionSetValue>("new_customerstatus").Value; var current_syn_customerid = targetEntity.GetAttributeValue<string>("new_customerid"); var current_syn_debtorclassid = targetEntity.GetAttributeValue<EntityReference>("new_customerclass").Id.ToString(); //Add attribute Guid contactId = Guid.Empty; //sepcify value targetEntity.Attributes.Add("primarycontactid", new EntityReference("contact", contactId)); }
to read pre or post image, please refer the following code, reading attribute value is same as above.var preEntity = (Entity)context.PreEntityImages[preimagename]; var postEntity = (Entity)context.PostEntityImages[postimagename];
Thomas T(MCBMSS) If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".- Edited by Thomas Thankachan Thursday, November 24, 2011 4:16 AM
- Proposed as answer by Thomas Thankachan Thursday, November 24, 2011 4:16 AM
- Marked as answer by DavidJennawayMVP, Moderator Thursday, January 12, 2012 1:50 PM
Thursday, November 24, 2011 4:00 AM -
Hi Thomas,
This was very helpful, thank you!
I know it's a change of subject but since you mentioned it in your code and I'm looking for it, how can we read the text value of the optionset?
Thursday, November 24, 2011 11:29 AM -
Hi,
You can use FormattedValues
myEntity.FormattedValues["paymenttermscode"].ToString()
<br/>
<br/>
Console.WriteLine("Value: {0}", ((OptionSetValue)account.Attributes["paymenttermscode"]).Value); Console.WriteLine("Value: {0}", account.GetAttributeValue<OptionSetValue>("paymenttermscode").Value); Console.WriteLine("Text: {0}", account.FormattedValues["paymenttermscode"].ToString());
Thomas T(MCBMSS) If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".- Proposed as answer by Thomas Thankachan Friday, November 25, 2011 1:50 AM
Friday, November 25, 2011 1:49 AM -
Hi Thomas,
FormattedValues is not working, it's throwing the exception "The given key was not present in the dictionary".
Any idea what could be the reason?
Tuesday, November 29, 2011 9:46 AM