AVIK DUTTA
AVIK DUTTA

Reputation: 746

How to add a new entity to MS CRM

I want to create an new entity in crm

    OrganizationService_orgService ;
    var connection = CrmConnection.Parse(conn);
    _orgService = new OrganizationService(connection);

    Entity newEntity = new Entity("this_is_a_new_entity");
    Guid newEntityID = _orgService.Create(newEntity); 

I wrote the above code where the conn is the connection string in the format which is correct (i checked)

  string conn = "Url=https://damnidiot.crm5.dynamics.com; [email protected]; Password=XXXXXXXXX;";

but when i run the code i get an exception {"The entity with a name = 'this_is_a_new_entity' was not found in the MetadataCache."}

i am asuming i got this error because my crm does not have defination for the entity this_is_a_new_entity .

Is it possible to retrive And update the metadata cache of my MS CRM ?(I AM USING Microsoft Dynamics CRM 2013 )

Upvotes: 0

Views: 993

Answers (2)

Alex
Alex

Reputation: 23310

If you use new Entity("new_entity_name") you are telling the code that you want to create a new record inside the already existing entity named new_entity_name.

To create a new entity altogether you have to issue a CreateEntityRequest (link to MSDN)

// PART OF THE LINKED SAMPLE
CreateEntityRequest createrequest = new CreateEntityRequest
{
    //Define the entity
    Entity = new EntityMetadata
    {
        SchemaName = _customEntityName,
        DisplayName = new Label("Bank Account", 1033),
        DisplayCollectionName = new Label("Bank Accounts", 1033),
        Description = new Label("An entity to store information about customer bank accounts", 1033),
        OwnershipType = OwnershipTypes.UserOwned,
        IsActivity = false,

    },

    // Define the primary attribute for the entity
    PrimaryAttribute = new StringAttributeMetadata
    {
        SchemaName = "new_accountname",
        RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
        MaxLength = 100,
        Format = StringFormat.Text,
        DisplayName = new Label("Account Name", 1033),
        Description = new Label("The primary attribute for the Bank Account entity.", 1033)
    }

};

Upvotes: 4

andriikulyk
andriikulyk

Reputation: 505

To create new entity you should use Create Entity Request. Your code creates record of this_is_a_new_entity.

Upvotes: 2

Related Questions