Search code examples
c#dynamics-crm-2011dynamics-crmdynamics-crm-2013

Is it possible to check if an Entity is "Activity" enabled in Dynamics CRM programatically?


When creating a custom Entity in Dynamics CRM 2013, you have the option of enabling Activities for that entity (i.e. in the Entity configuration screen, you can check the "Activities" checkbox).

This allows Activity records to be linked to the new custom Entity.

We have a CRM plugin developed in C# that needs to check if a custom entity is "Activity" enabled, as the plugin needs to create an Activity record linked to the custom entity record.

Is there a way to check if an Entity is "Activity" enabled programatically?


Solution

  • You can use the RetrieveEntityRequest to find out if an entity is enabled for activities. The RetrieveEntityResponse contains an EntityMetadata object with in it the OneToManyRelationships.

    When the entity has a relationship with activitypointer entities, it is enabled for activities.

    Code example:

    var metaResponse = (RetrieveEntityResponse)proxy.Execute(new RetrieveEntityRequest
    {
        EntityFilters = EntityFilters.Relationships,
        LogicalName = "account",
        RetrieveAsIfPublished = false
    });
    
    bool isActivityEnabled =
        metaResponse.EntityMetadata.OneToManyRelationships
        .Any(r => r.ReferencingEntity == "activitypointer");
    

    Do not set RetrieveAsIsPublished = true. Of course setting this property can as well be omitted, but I left it in sake of clarity. Credits go to SimonM, see comments section below.