How to get the entity schema name list registered in your Dynamics CRM Organization using C#.

Use the following code for fetching entity list names in your console application:


  public static EntityMetadata[] GetEntities(IOrganizationService organizationService)
        {
            Dictionary attributesData = new Dictionary();
            RetrieveAllEntitiesRequest metaDataRequest = new RetrieveAllEntitiesRequest();
            RetrieveAllEntitiesResponse metaDataResponse = new RetrieveAllEntitiesResponse();
            metaDataRequest.EntityFilters = EntityFilters.Entity;

            // Execute the request.

            metaDataResponse = (RetrieveAllEntitiesResponse)organizationService.Execute(metaDataRequest);

            var entities = metaDataResponse.EntityMetadata;

            return entities;
        }

        public static void PrintEntitySchemaNames()
        {
            OrganizationServiceProxy _orgservice = CreateCrmServiceObjectOnline();
            Console.WriteLine("===================================");

            #region GivesTHeListOfAllEntitiesInAnOrganization
            var allEntities = GetEntities(_orgservice);
            foreach (EntityMetadata Entity in allEntities)
            {
                Console.WriteLine(Entity.LogicalName);
            }
            Console.WriteLine("===================================");
            Console.ReadKey();
            #endregion
        }


 public static OrganizationServiceProxy CreateCrmServiceObjectOnPremise()
        {

            OrganizationServiceProxy srvOnline = null;
            string strServerUrl = "YourServerUrl + "/" + OrgName";
            string userId = "UserName";
            string password = "passWord";
            string DomainName = "Domain";

            Uri orgUri;
            orgUri = new Uri(strServerUrl + "/XRMServices/2011/Organization.svc");

            IServiceManagement orgServiceManagement =
             ServiceConfigurationFactory.CreateManagement(orgUri);

            AuthenticationCredentials creds = new AuthenticationCredentials();
            creds.ClientCredentials.UserName.UserName = userId;
            creds.ClientCredentials.UserName.Password = password;
            AuthenticationCredentials tokenCredentials = orgServiceManagement.Authenticate(creds);

            ClientCredentials credentials = new ClientCredentials();

            credentials.Windows.ClientCredential = new System.Net.NetworkCredential(userId, password, DomainName);

            try
            {
                srvOnline = new OrganizationServiceProxy(orgUri, null, credentials, null);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error Occurs: " + ex.Message);
            }

            return srvOnline;
        }

//If your CRM is Online
        public static OrganizationServiceProxy CreateCrmServiceObjectOnline()
        {
            OrganizationServiceProxy oService = null;
            string UrlPrefix = string.Empty;
            string ServerUrl = string.Empty;
            string DomainName = string.Empty;
            string DomainUserId = string.Empty;
            string DomainPassword = string.Empty;

            UrlPrefix = "https://";
            ServerUrl = "YourOrg.crmX.dynamics.com";
            DomainUserId = "user@YourOrgdstcomm.onmicrosoft.com";
            DomainPassword = "passWord";

            ClientCredentials credentials = new ClientCredentials();

            credentials.UserName.UserName = DomainUserId;
            credentials.UserName.Password = DomainPassword;
            Uri OrganizationUri = new Uri(UrlPrefix + ServerUrl + "/XRMServices/2011/Organization.svc");
            try
            {
                oService = new OrganizationServiceProxy(OrganizationUri, null, credentials, null);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }

            return oService;
        }

Comments

Popular posts from this blog

How to show enlarge image when mouse hover on image or link in Asp.Net(c#) using JavaScript

StateCode and StatusCode Values for mostly used entities in Microsoft Dynamics CRM 2013

How to add custom filter lookup field based on OptionSet field selection in Dynamics CRM.

go to top image