To open an entity record based on its ID in Dynamics 365 Customer Engagement (D365 CE) from a plugin, you'll need to use the SDK and some specific code to interact with the platform.
Here are the steps to do this.
- Set up your plugin project: Ensure you have a Dynamics 365 CE SDK and the necessary assemblies referenced in your project.
- Retrieve the entity record: Use the IOrganizationService to retrieve the record by its ID.
- Create a URL to open the record: Use the record ID to form a URL that will open the record in the Dynamics 365 web client.
Here is an example of how to do this.
Step 1. Set up your plugin project
Ensure you have the following assemblies referenced in your project.
Microsoft.Xrm.Sdk
Microsoft.Crm.Sdk.Proxy
Microsoft.Xrm.Tooling.Connector (if needed)
Step 2. Retrieve the entity record
Use the IOrganizationService to retrieve the record by its ID in the Execute method of your plugin.
public void Execute(IServiceProvider serviceProvider)
{
// Obtain 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);
// The ID of the entity to open
Guid entityId = new Guid("YOUR_ENTITY_ID_HERE");
// The logical name of the entity
string entityLogicalName = "YOUR_ENTITY_LOGICAL_NAME_HERE";
// Retrieve the entity
Entity entity = service.Retrieve(entityLogicalName, entityId, new ColumnSet(true));
// Create the URL to open the record
string entityUrl = GenerateEntityUrl(context, entityLogicalName, entityId);
// Use the tracing service to output the URL (for debugging purposes)
tracingService.Trace("Entity URL: " + entityUrl);
// You can now use this URL as needed
}
Step 3. Generate the URL to open the record
The method to generate the URL can be something like this.
private string GenerateEntityUrl(IPluginExecutionContext context, string entityLogicalName, Guid entityId)
{
// Base URL of your D365 CE instance
string baseUrl = context.OrganizationName;
// Construct the URL to open the entity record
string entityUrl = $"{baseUrl}/main.aspx?etn={entityLogicalName}&pagetype=entityrecord&id={entityId}";
return entityUrl;
}
Notes
- Context and Configuration: The base URL (baseUrl in the example) may need to be configured or retrieved dynamically depending on your environment setup.
- Security: Ensure the user context under which the plugin runs has appropriate permissions to access the entity records.
This code will help you construct a URL to directly open an entity record in Dynamics 365 CE from within a plugin. Adjust the entity logical name and entity ID as required for your specific scenario.