Details:
Can you help me?
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
public class PreventAnnotationDeletion : IPlugin
{
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);
if (context.MessageName.ToLower() != "delete" || context.PrimaryEntityName.ToLower() != "annotation")
return;
Guid annotationId = context.PrimaryEntityId;
Entity annotation = service.Retrieve("annotation", annotationId, new ColumnSet("ownerid", "createdby"));
if (annotation != null)
{
EntityReference owner = annotation.GetAttributeValue<EntityReference>("ownerid");
EntityReference createdBy = annotation.GetAttributeValue<EntityReference>("createdby");
if (owner.Id != createdBy.Id)
{
throw new InvalidPluginExecutionException("Deletion is not allowed. The owner and the created by fields do not match.");
}
}
}
}