Search code examples
c#dynamics-crmdynamics-crm-2016dynamics-crm-365

Add distinctive entity sub components to a Dynamics CRM solution


I am working on a utility in which i am creating a rollback solution based on the provided target solution. As of now the utility is working fine, and it reads the solution to be deployed on target org and creates a new rollback solution on target org with all the necessary components as entity, webresources, SDK steps, security roles, workflows etc from the target org. I have used SDK's AddSolutionComponentRequest class to achieve this.

When the utility detects an entity in the solution it simply adds the whole entity with all the metadata like all fields, views, forms etc.

CRM 2016 introduced the feature of solution segmentation through which we can specifically add those entity components which have been changed.

How can I leverage this feature in my utility as i have not found yet any API method allowing me to add specific entity components to the solution.


Solution

  • For a segmented solution components of type Entity must be added to the solution with the DoNotIncludeSubcomponents option set to true. Then, distinctive parts of the entity can be added to the solution one by one.

    An example where entity "account" is added to solution "Test" with only attribute "accountnumber":

    private static EntityMetadata RetrieveEntity(string entityName, IOrganizationService service)
    {
        var request = new RetrieveEntityRequest
        {
            LogicalName = entityName,
            EntityFilters = EntityFilters.All
        };
    
        return ((RetrieveEntityResponse)service.Execute(request)).EntityMetadata;
    }
    
    private static void AddEntityComponent(Guid componentId, int componentType, string solutionName, IOrganizationService service)
    {
        var request = new AddSolutionComponentRequest
        {
            AddRequiredComponents = false,
            ComponentId = componentId,
            ComponentType = componentType,
            DoNotIncludeSubcomponents = true,
            SolutionUniqueName = solutionName
        };
    
        service.Execute(request);
    }
    
    IOrganizationService service = factory.CreateOrganizationService(null);
    
    EntityMetadata entity = RetrieveEntity("account", service);
    AddEntityComponent(entity.MetadataId.Value, 1, "Test", service);
    AddEntityComponent(entity.Attributes.First(a => a.LogicalName == "accountnumber").MetadataId.Value, 2, "Test", service);