web
You’re offline. This is a read only version of the page.
close
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.

I have the same question (0)
  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Can you please share your code?

    Please check code from below article to retrieve attribute metadata

    carldesouza.com/.../

  • Suggested answer
    Steve Zhao2 Profile Picture
    on at

    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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 62 Most Valuable Professional

#2
#ManoVerse Profile Picture

#ManoVerse 57

#3
Pallavi Phade Profile Picture

Pallavi Phade 49

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans