InputParameters["Target"]
Target is the entity record that the plugin is executed against.It is late bound, but you can use
ToEntity()
to convert it into early bound instance. One thing useful to know about this instance is that it only contains “dirty” attributes.
Unchanged fields will simply not be there, if you converted to early bound, the value of the attribute will be
null
.Sample code (without using developer toolkit)
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
OrganizationServiceContext orgContext = new OrganizationServiceContext(service);
if (tracingService == null)
{
throw new InvalidPluginExecutionException("Failed to retrieve the tracing service.");
}
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
try
{
//Do your custom logic here
}
catch (Exception Ex)
{
throw new InvalidPluginExecutionException(Ex.Message);
}
}
//throw new NotImplementedException();
}
Sample code(using Developer Toolkit)
protected void ExecutePreValidateAccountCreate(LocalPluginContext localContext){
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
IPluginExecutionContext context = localContext.PluginExecutionContext;
IOrganizationService service = localContext.OrganizationService;
var orgServiceContext = new OrganizationServiceContext(service);
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parmameters.
Entity entity = (Entity)context.InputParameters["Target"];
var newAccount = entity.ToEntity<Account>();
try
{
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}
// TODO: Implement your custom Plug-in business logic.
}
References
http://sliong.wordpress.com/2012/06/06/crm-2011-event-execution-pipeline-and-target-input-parameters/