Thursday, December 6, 2012

Attribute and picklist / option set metadata in CRM 2011


A customer requested a list of attribute names and their associated label for a particular CRM entity. I just used a SQL query to get the info and cut-and-pasted into Excel. Easy…
01SELECT DISTINCT a.Name, l.Label
02FROM MetadataSchema.Attribute a
03INNER JOIN MetadataSchema.Entity e
04ON a.EntityId = e.EntityId
05AND YEAR(e.OverwriteTime) = 1900
06INNER JOIN MetadataSchema.LocalizedLabel l
07ON a.AttributeId = l.ObjectId
08AND l.ObjectColumnName='DisplayName'
09AND l.LanguageId = 1033
10WHERE e.Name 'opportunity'
Then they asked for a list of all picklist values and labels on the system. A slightly more complex query for that but it was fun figuring out the MetadataSchema tables.
01SELECT DISTINCT e.Name as Entity, a.Name as Attribute, pl.Value, l.Label
02FROM MetadataSchema.Attribute a
03INNER JOIN MetadataSchema.Entity e
04ON a.EntityId = e.EntityId
05AND YEAR(e.OverwriteTime) = 1900
06INNER JOIN MetadataSchema.AttributeTypes t
07ON a.AttributeTypeId = t.AttributeTypeId
08AND t.Description = 'picklist'
09INNER JOIN MetadataSchema.AttributePicklistValue pl
10ON a.OptionSetId = pl.OptionSetId
11INNER JOIN MetadataSchema.LocalizedLabel l
12ON pl.AttributePicklistValueId = l.ObjectId
13ORDER BY e.Name, a.Name
New: Full entity metadata export from SQL
01SELECT
02  a.AttributeId,
03  at.Description as AttributeType,
04  a.Name,
05  a.Length,
06  l.Label,
07  d.Label as Description
08FROM MetadataSchema.Attribute a
09INNER JOIN MetadataSchema.AttributeTypes at
10  ON  a.AttributeTypeId = at.AttributeTypeId
11INNER JOIN MetadataSchema.LocalizedLabel l
12  ON  a.AttributeId = l.ObjectId
13  AND YEAR(l.OverwriteTime) = 1900
14  AND l.ObjectColumnName='DisplayName'
15LEFT JOIN MetadataSchema.LocalizedLabel d
16  ON  a.AttributeId = d.ObjectId
17  AND YEAR(d.OverwriteTime) = 1900
18  AND d.ObjectColumnName='Description'
19WHERE a.EntityId = (SELECT EntityId FROM MetadataSchema.Entity WHERE Name='Account')
20  AND YEAR(a.OverwriteTime) = 1900
21ORDER BY l.Label

No comments:

Post a Comment