Difference of this approach is we retrieve entity reference instead of entity. Then we can retrieve the other attributes through retrieve method since we got the Id. (While we are in Pre stage we still have time to grab them.)
Check the code as below;
using System; using System.Collections.Generic; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using System.ServiceModel; namespace TestCompany.CRM.Plugin { public class officePreDelete : IPlugin { public void Execute(IServiceProvider serviceProvider) { IPluginExecutionContext context; IOrganizationServiceFactory factory; IOrganizationService service; try { context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference) { EntityReference EntityRef = (EntityReference)context.InputParameters["Target"]; if (EntityRef.LogicalName != "new_office") return; factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); service = factory.CreateOrganizationService(context.UserId); // Do the logic as required // use EntityRef.Id retrive other attributes } } catch (FaultException<OrganizationServiceFault> e) { throw e; } finally { service = null; factory = null; context = null; } } } }
Now check the plug-in registration setting;
Important Note
Pl note, though we are in pre-Delete stage, record has already lost the relationships with other records. This means you are not able to query other records using the values in deleting record. If you need to do, what needs to do is register the plug-in step in Pre-validation stage than Pre-operation.
Pl note, though we are in pre-Delete stage, record has already lost the relationships with other records. This means you are not able to query other records using the values in deleting record. If you need to do, what needs to do is register the plug-in step in Pre-validation stage than Pre-operation.
Related posts;
Sample Plug-in: Compare Pre and Post images on Update Sample Plug-in code: Create and Update
Retrieve attributes of entity object
