Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

How to get custom metadata from entity using c# console or plugin

(0) ShareShare
ReportReport
Posted on by 40

Hello All,

I have  create a publisher and name is Employee and its prefix is emp.

I have try to get all custom fields using console application in which prefix emp and field is emp_fieldname but i am not getting tis type of data.

Please help me.

  • Suggested answer
    Steve Zhao2 Profile Picture
    on at
    RE: How to get custom metadata from entity using c# console or plugin

    Hi vijay1234567,

    I create a publisher and add a custom field whose prefix is emp on the Account table.

    pastedimage1667375335337v2.png

    I tested with the following code and can get the data successfully.

    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Metadata;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Tooling.Connector;
    using System;
    using System.Linq;
    
    namespace ReteriveMetadata
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    var connectionString = @"AuthType = Office365; 
                    Url = https://yourcrm.crm.dynamics.com/;Username=yourusername;Password=yourpassword";
                    CrmServiceClient client = new CrmServiceClient(connectionString);
    
                    IOrganizationService service;
                    service = (IOrganizationService)client.OrganizationWebProxyClient != null ? (IOrganizationService)client.OrganizationWebProxyClient : (IOrganizationService)client.OrganizationServiceProxy;
    
                    const string ENTITY_TO_CHECK = "account";   // logical name
    
                    var prefixesQuery = new QueryExpression("publisher")
                    {
                        ColumnSet = new ColumnSet("customizationprefix")
                    };
                    var prefixesData = client.RetrieveMultiple(prefixesQuery).Entities
                      ?.Select(e => e.GetAttributeValue("customizationprefix"));
    
                    Console.WriteLine("Publisher prefixes:");
                    foreach (var pd in prefixesData)
                        Console.WriteLine("> {0}", pd);
    
                    var columnsQuery = new RetrieveEntityRequest
                    {
                        LogicalName = ENTITY_TO_CHECK,
                        EntityFilters = EntityFilters.Attributes,
                        RetrieveAsIfPublished = true
                    };
    
                    var columnsData = (client.Execute(columnsQuery) as RetrieveEntityResponse)?.EntityMetadata?.Attributes
                      ?.Where(col => prefixesData.Any(prefix => col.LogicalName.StartsWith("emp_")))
                      .OrderBy(col => col.LogicalName);
    
                    Console.WriteLine("{0} custom fields: {1}", ENTITY_TO_CHECK, columnsData.Count());
                    foreach (var cd in columnsData)
                        Console.WriteLine("> {0}", cd.LogicalName);
    
                    Console.ReadLine();
                }
                catch (Exception ex)
                { }
    
            }
        }
    }
    

    Result:

    pastedimage1667375120704v1.png

    You can have a try the above code.

    Reference:

    c# - How can recognize a custom field programmatically? - Stack Overflow

  • Suggested answer
    Bipin D365 Profile Picture
    28,961 Moderator on at
    RE: How to get custom metadata from entity using c# console or plugin

    Hi,

    Can you please share your code?

    Please check code from below article to retrieve attribute metadata

    carldesouza.com/.../

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,001 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,837 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans