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…
SELECT DISTINCT a.Name, l.Label FROM MetadataSchema.Attribute a INNER JOIN MetadataSchema.Entity e ON a.EntityId = e.EntityId AND YEAR(e.OverwriteTime) = 1900 INNER JOIN MetadataSchema.LocalizedLabel l ON a.AttributeId = l.ObjectId AND l.ObjectColumnName='DisplayName' AND l.LanguageId = 1033 WHERE 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.
SELECT DISTINCT e.Name as Entity, a.Name as Attribute, pl.Value, l.Label FROM MetadataSchema.Attribute a INNER JOIN MetadataSchema.Entity e ON a.EntityId = e.EntityId AND YEAR(e.OverwriteTime) = 1900 INNER JOIN MetadataSchema.AttributeTypes t ON a.AttributeTypeId = t.AttributeTypeId AND t.Description = 'picklist' INNER JOIN MetadataSchema.AttributePicklistValue pl ON a.OptionSetId = pl.OptionSetId INNER JOIN MetadataSchema.LocalizedLabel l ON pl.AttributePicklistValueId = l.ObjectId ORDER BY e.Name, a.Name
New: Full entity metadata export from SQL
SELECT a.AttributeId, at.Description as AttributeType, a.Name, a.Length, l.Label, d.Label as Description FROM MetadataSchema.Attribute a INNER JOIN MetadataSchema.AttributeTypes at ON a.AttributeTypeId = at.AttributeTypeId INNER JOIN MetadataSchema.LocalizedLabel l ON a.AttributeId = l.ObjectId AND YEAR(l.OverwriteTime) = 1900 AND l.ObjectColumnName='DisplayName' LEFT JOIN MetadataSchema.LocalizedLabel d ON a.AttributeId = d.ObjectId AND YEAR(d.OverwriteTime) = 1900 AND d.ObjectColumnName='Description' WHERE a.EntityId = (SELECT EntityId FROM MetadataSchema.Entity WHERE Name='Account') AND YEAR(a.OverwriteTime) = 1900 ORDER BY l.Label
guest
/ October 26, 2011Excellent! Thank you for sharing 🙂
Peter Hale
/ February 7, 2012Hi Pete
scripts 1 and 2 work fine – the third full one give an error
Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, , >= or when the subquery is used as an expression.
Regards
Another Pete
onlysuresh
/ September 19, 2014Add DISTINCT for sub query.
SELECT DISTINCT EntityId FROM MetadataSchema.Entity WHERE Name=’Account’
Maarten Docter
/ February 13, 2012Hi Pete,
Thank you for sharing! Great work!
read more
/ December 24, 2012Howdy I am so thrilled I found your weblog, I really found you by mistake, while I was looking on Askjeeve for something
else, Anyhow I am here now and would just like to say
many thanks for a remarkable post and a all round entertaining blog (I also love
the theme/design), I don’t have time to read through it all at the minute but I have saved it and also included your RSS feeds, so when I have time I will be back to read much more, Please do keep up the excellent job.
petecrm2011
/ January 10, 2013Thank you very much. I’m glad you like it.
haarausfall
/ May 3, 2013Asking questions are really good thing if you are not understanding something entirely, except this paragraph provides pleasant understanding yet.
David cat
/ May 13, 2013Thanks for your post! Really helpful.
However, I have a question for you: How would you list only the fields being used in the CRM? (Only the ones appearing in the forms)
Yvonne
/ May 21, 2013I blog frequently and I seriously thank you for your content.
The article has really peaked my interest. I’m going to bookmark your blog and keep checking for new information about once a week. I subscribed to your RSS feed too.
J. Hogue
/ October 24, 2013Rewrite of third script:
SELECT
a.AttributeId,
a.Name,
at.Description as AttributeType,
a.Length,
l.Label,
d.Label as Description
FROM MetadataSchema.Entity e
INNER JOIN MetadataSchema.Attribute a
ON a.EntityId = E.EntityId
AND YEAR(a.OverwriteTime) = 1900
INNER JOIN MetadataSchema.AttributeTypes at
ON a.AttributeTypeId = at.AttributeTypeId
INNER JOIN MetadataSchema.LocalizedLabel l
ON a.AttributeId = l.ObjectId
AND YEAR(l.OverwriteTime) = 1900
AND l.ObjectColumnName=’DisplayName’
LEFT JOIN MetadataSchema.LocalizedLabel d
ON a.AttributeId = d.ObjectId
AND YEAR(d.OverwriteTime) = 1900
AND d.ObjectColumnName=’Description’
WHERE e.Name=’Account’
CuriousCRMer
/ March 10, 2014If I have an option set that is not on any entity, how can I retrieve its options via c#? I can see my new optionset in OptionSetLogicalView, but not the options themselves, and StringMap seems to assume that the optionset is related to a particular entity.