I'm in the process of trying to write a Plugin to audit READs on CRM data ... the inbuilt auditing will only audit Create, Update or Deletes.
Reading around the subject I gather that I need to register a step in my plugin against the "RetrieveMultiple" message for Entities that I'm interested in.
I've created an Audit entity with the attributes:
xcor_auditid, Primary Key
xcor_entityreferenceguid, Single line of text (string representation of the Guid)
xcor_entityreferenceid, Single line of text (our unique reference number for the entityreference)
xcor_entitytype, single line of text ( the logical name of the entity)
xcor_operation, single line of text (CRUD operation - RetrieveMultiple in this case)
Now I'm capturing the RetrieveMultiple message in the Post-Operation step and I'm able to capture the EntityType from the context.PrimaryEntityType, however, I'd expect the context.PrimaryEntityId to be populated but it appears to be an empty Guid.
I've played around with various options but I'm unable to get the Guid of the retrieved record.
I have tried looking for "Target" in context.InputParameters and "BusinessEntity" in context.OutputParameters but neither exist. In fact the only In/OutputParameter is "Query" of type QueryExpression.
I'm using CRM 2011 Rollup 12.
Here's my code
public class Plugin : IPlugin
{ public void Execute(IServiceProvider serviceProvider)
{
if (serviceProvider == null)
{
throw new ArgumentNullException("serviceProvider");
}
// Get a reference to the tracing service.
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
string[] operationsToAudit = new string[]{"Retrieve", "RetrieveMultiple"};
if (operationsToAudit.Contains(context.MessageName))
{
Entity audit = new Entity("xcor_audit");
audit["xcor_operation"] = context.MessageName;
audit["xcor_entitytype"] = context.PrimaryEntityName;
//audit["xcor_entityreferenceid"] = "????";
audit["xcor_entityreferenceguid"] = context.PrimaryEntityId.ToString();
service.Create(audit); }
}
catch (FaultException<OrganizationServiceFault> e)
{
tracingService.Trace(string.Format(CultureInfo.InvariantCulture, "Exception: {0}", e.ToString()));
// Handle the exception.
throw;
}
finally
{
tracingService.Trace(string.Format(CultureInfo.InvariantCulture, "Exiting Plugin.Execute()"));
}
}
}
If anyone has any ideas they would be much appreciated. Alternatively, if you've got any sample code to perform a similar function I'd love to see it.
Thanks, Rob