Search code examples
c#dynamics-crmmicrosoft-dynamics

Dynamics 365: How to get the Form Metadata?


How to get Form Metadata via C#? I need to get info about all OnChange event handlers of the elements of specific Form (look at the screen on the bottom) throught console application (written on C#). I know how to get Entity Metadata, but I doesn't see any info about its forms inside of.

enter image description here


Solution

  • These are not stored in Database for readily accessible as these are stored in formXML and quite not sure if we can access the events in easy way.

    Still you can query the Entity form metadata for formXML/formJson and parse it for own usage.

    https://crmorg.crm.dynamics.com/api/data/v9.1/systemforms?$select=formjson,formxml&$filter=name%20eq%20%27Account%27
    

    Referred below from this blog

    enter image description here

    That blog have a sql query pulling the formXML out of on-prem database.

    WITH x AS 
    (
           SELECT FormId, e.Name, CAST(FormXml AS Xml) FormXml 
           FROM SystemForm sf
           JOIN Entity e ON sf.ObjectTypeCode = e.ObjectTypeCode AND e.OverwriteTime = '1900-01-01 00:00:00.000'
           WHERE e.Name = 'Account'
    )
    , y AS 
    (
           SELECT 
                  x.FormId,
                  x.Name Entity, 
                  x.FormXml, -- Uncomment to see the full form xml
                  t.c.value('@name', 'varchar(max)') HandlerName,
                  t.c.value('@attribute', 'varchar(max)') AttributeName,
                  a.b.value('@libraryName', 'varchar(max)') Library,
                  a.b.value('@functionName', 'varchar(max)') FunctionName,
                  a.b.value('@enabled', 'varchar(16)') [Enabled],
                  t.c.query('.') [t.c.query], 
                  a.b.query('.') [a.b.query]
           FROM  x
           CROSS APPLY x.FormXml.nodes('/form/events/event') T(c)
           OUTER APPLY t.c.nodes('Handlers/Handler') a(b)
           WHERE t.c.value('count(Handlers/Handler)', 'int') > 0
    )
    
    SELECT 
           *
    FROM y 
    -- WHERE Enabled = 'true' -- This will exclude non-attribute related rows
    ORDER BY y.Entity, y.HandlerName, y.Library, y.FunctionName