Search code examples
sqllabeldynamics-crmreporting

How to join dbo.LocalizedLabelView to get form labels in Dynamics CRM?


In Dynamics CRM, I often get requirements from business users to create reports. Business users know and speak about entity display names and attribute labels. To write a query, I need to map those to entity names and attribute names. I would like to use a query to look this up.

To what do I join the dbo.LocalizedLabelView view to get the AttributeLabel column in the following query? I can't figure out what ObjectId is supposed to reference. (And if you can tell me how you figured out the answer I'd be especially appreciative!)

select
    [EntityName]           = entityNames.Name,
    [EntityDisplayName]    = entityDisplayNames.Label,
    [AttributeName]        = attributeNames.PhysicalName,
    [AttributeDisplayName] = attributeDisplayNames.Label
    --[AttributeLabel]     = attributeLabels.Label
from 
    dbo.EntityView entityNames

    inner join dbo.LocalizedLabelView entityDisplayNames
        on entityDisplayNames.ObjectId = entityNames.EntityId
        and entityDisplayNames.ObjectColumnName = 'LocalizedName'

    left outer join dbo.AttributeView attributeNames
        on attributeNames.EntityID = entityNames.EntityID

    inner join dbo.LocalizedLabelView attributeDisplayNames
        on attributeDisplayNames.ObjectId = attributeNames.AttributeID
        and attributeDisplayNames.ObjectColumnName = 'DisplayName'
        and attributeDisplayNames.LanguageID = entityDisplayNames.LanguageID

    --inner join dbo.LocalizedLabelView attributeLabels
    --  on attributeLabels.ObjectId = ?????
    --  and attributeLabels.LanguageID = entityDisplayNames.LanguageID
where
    entityDisplayNames.LanguageID = 1033
order by
    entityDisplayNames.Label,
    attributeDisplayNames.Label

Solution

  • ObjectId is a reference to the internal ID of a thing in the CRM database. This thing can be an attribute, entity, label, or whatever.

    Since you want the label for your attribute, use that attribute's id as the ObjectId here. I think that you want your join condition to look like this:

    inner join dbo.LocalizedLabelView attributeLabels
        on attributeLabels.ObjectId = attributeNames.AttributeID
        and attributeLabels.LanguageID = entityDisplayNames.LanguageID
        and attributeLabels.ObjectColumnName = 'DisplayName'
    

    If you want the description for the attribute, you can change the ObjectColumnName to 'Description'.