Friday 13 April 2012

Create Custom Entity and Attribute using C# in CRM 2011

In this article , i am explaining how to create custom entity and attribute using c# code
Namespace need to include
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;
Custom Code to Create Entity and Attribute
using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri,
       serverConfig.HomeRealmUri,
       serverConfig.Credentials,
       serverConfig.DeviceCredentials))
{
       _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

       // Create custom entity.
       CreateEntityRequest _entity = new CreateEntityRequest()
       {
          Entity = new EntityMetadata
          {
             LogicalName = "new_payment",
             DisplayName = new Label("Payment", 1033),
             DisplayCollectionName = new Label("Payment", 1033),
             OwnershipType = OwnershipTypes.UserOwned,
             SchemaName = "New_Payment",
             IsActivity = false,
             IsAvailableOffline = true,
             IsAuditEnabled = new BooleanManagedProperty(true),
             IsMailMergeEnabled = new BooleanManagedProperty(false)
           },
           HasActivities = false,
           HasNotes = true,
           PrimaryAttribute = new StringAttributeMetadata()
           {
              SchemaName = "Description",
              LogicalName = "description",
              RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
              MaxLength = 100,
              DisplayName = new Label("Description", 1033)
           }
       };
       _serviceProxy.Execute(_entity);

       // Create custom attributes.
       CreateAttributeRequest attrReq = new CreateAttributeRequest()
       {
           Attribute = new StringAttributeMetadata()
           {
              LogicalName = "new_identity",
              DisplayName = new Label("Identity", 1033),
              SchemaName = "New_Identity",
              MaxLength = 500,
              RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.Recommended),
              IsSecured = true
            },
            EntityName = "new_payment"
       };
       CreateAttributeResponse identityAttributeResponse = (CreateAttributeResponse)_serviceProxy.Execute(attrReq);
       Guid _identityId = identityAttributeResponse.AttributeId;

       attrReq = new CreateAttributeRequest()
       {
          Attribute = new StringAttributeMetadata()
          {
             LogicalName = "new_message",
             DisplayName = new Label("Message", 1033),
             SchemaName = "New_Message",
             MaxLength = 140,
             RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.Recommended),
             IsSecured = true
           },
           EntityName = "new_payment"
       };

       // Execute the request.
       CreateAttributeResponse messageAttributeResponse = (CreateAttributeResponse)_serviceProxy.Execute(attrReq);
       Guid _messageId = messageAttributeResponse.AttributeId;

}

No comments:

Post a Comment