How to create and publish Entity MS CRM (C#.NET)

To connect CRM find my blog here

Only Code No Talks.

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Tooling.Connector;
using System;
using System.Configuration;
namespace CreateEntityBlog
{
 class Program
 {
 static void Main(string[] args)
 {
 CrmServiceClient crmServiceClientObj = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CrmOnlineStringFromAppConfig"].ConnectionString);
 if (!crmServiceClientObj.IsReady)
 Console.WriteLine("No Connection was Made.");
 Console.WriteLine("Connected");
 string entityName = "new_Adviser";
 CreateEntityRequest createrequest = new CreateEntityRequest
 {
 Entity = new EntityMetadata
 {
 SchemaName = entityName,
 DisplayName = new Label("Adviser", 1033),
 DisplayCollectionName = new Label("Advisers", 1033),
 Description = new Label("An entity to store information about Advisers", 1033),
 OwnershipType = OwnershipTypes.UserOwned,
 IsActivity = false
 },
 PrimaryAttribute = new StringAttributeMetadata
 {
 SchemaName = "new_advisername",
 RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
 MaxLength = 100,
 FormatName = StringFormatName.Text,
 DisplayName = new Label("Adviser Name", 1033),
 Description = new Label("The primary attribute for the Adviser entity.", 1033)
 },
 };
 PublishXmlRequest publishEntityRequest = new PublishXmlRequest
 {
 ParameterXml = String.Format("{0}", entityName.ToLower())
 };
 try
 {
 var result = crmServiceClientObj.Execute(createrequest);
 if (result.Results != null)
 Console.WriteLine("Adviser Entity Created");
 crmServiceClientObj.Execute(publishEntityRequest);
 }
 catch (Exception e) { Console.WriteLine("Failed to Create Entity. \nReason {0}", e.Message); }
 Console.ReadLine();
 }
 }
}

3 thoughts on “How to create and publish Entity MS CRM (C#.NET)

Leave a comment