locked
create custom entity and its attribute RRS feed

  • Question

  • Hi,

    Can anyone please check my code once.

    I am getting this error


             private OrganizationServiceProxy _serviceProxy;

            private const String _customEntityName = "new_supplier";

            public void Run(ServerConnection.Configuration serverConfig, bool promptForDelete)
            {
                try
                {               
                    using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials))
                    {                  
                        _serviceProxy.EnableProxyTypes();


                        // Create the custom entity.
                        //<snippetCreateUpdateEntityMetadata1>
                        CreateEntityRequest createrequest = new CreateEntityRequest
                        {

                            //Define the entity
                            Entity = new EntityMetadata
                            {
                                SchemaName = _customEntityName,
                                DisplayName = new Label("Supplier", 1033),
                                DisplayCollectionName = new Label("Supplier", 1033),
                                Description = new Label("An entity to store information about Supplier", 1033),
                                OwnershipType = OwnershipTypes.UserOwned,
                                IsActivity = false,

                            },

                            // Define the primary attribute for the entity
                            PrimaryAttribute = new StringAttributeMetadata
                            {
                                SchemaName = "new_name",
                                RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
                                MaxLength = 100,
                                FormatName = StringFormatName.Text,
                                DisplayName = new Label("Supplier Name", 1033),
                                Description = new Label("The primary attribute for the Supplier entity.", 1033)
                            }

                        };
                        _serviceProxy.Execute(createrequest);
                        Console.WriteLine("The Supplier entity has been created.");
                       
                        CreateAttributeRequest createBankNameAttributeRequest = new CreateAttributeRequest
                        {
                            EntityName = _customEntityName,
                            Attribute = new StringAttributeMetadata
                            {
                                SchemaName = "new_suppliername",
                                RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
                                MaxLength = 100,
                                FormatName = StringFormatName.Text,
                                DisplayName = new Label("Supplier Name", 1033),
                                Description = new Label("The name of the Supplier.", 1033)
                            }
                        };

                        _serviceProxy.Execute(createBankNameAttributeRequest);
                        //</snippetCreateUpdateEntityMetadata2>
                        Console.WriteLine("An Supplier name attribute has been added to the Supplier entity.");


                    
                        CreateAttributeRequest createCheckedDateRequest = new CreateAttributeRequest
                        {
                            EntityName = _customEntityName,
                            Attribute = new StringAttributeMetadata
                            {
                                SchemaName = "new_suppliernumber",
                                RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
                                MaxLength = 100,
                                FormatName = StringFormatName.Text,
                                DisplayName = new Label("Supplier Number", 1033),
                                Description = new Label("Supplier Number", 1033)

                            }
                        };

                        _serviceProxy.Execute(createCheckedDateRequest);
                        Console.WriteLine("An birthdate attribute has been added to student entity.");
                      
                        CreateOneToManyRequest req = new CreateOneToManyRequest()
                        {
                            Lookup = new LookupAttributeMetadata()
                            {
                                Description = new Label("The owner of the student entity", 1033),
                                DisplayName = new Label("Supplier Owner", 1033),
                                LogicalName = "new_parent_contactid",
                                SchemaName = "New_Parent_ContactId",
                                RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired)
                            },
                            OneToManyRelationship = new OneToManyRelationshipMetadata()
                            {
                                AssociatedMenuConfiguration = new AssociatedMenuConfiguration()
                                {
                                    Behavior = AssociatedMenuBehavior.UseCollectionName,
                                    Group = AssociatedMenuGroup.Details,
                                    Label = new Label("Supplier", 1033),
                                    Order = 10000
                                },
                                CascadeConfiguration = new CascadeConfiguration()
                                {
                                    Assign = CascadeType.Cascade,
                                    Delete = CascadeType.Cascade,
                                    Merge = CascadeType.Cascade,
                                    Reparent = CascadeType.Cascade,
                                    Share = CascadeType.Cascade,
                                    Unshare = CascadeType.Cascade
                                },
                                ReferencedEntity = Contact.EntityLogicalName,
                                ReferencedAttribute = "contactid",
                                ReferencingEntity = _customEntityName,
                                SchemaName = "new_contact_new_supplier"
                            }
                        };
                        _serviceProxy.Execute(req);

                        CreateAttributeRequest createEntityImageRequest = new CreateAttributeRequest
                        {
                            EntityName = _customEntityName,
                            Attribute = new ImageAttributeMetadata
                            {
                                SchemaName = "EntityImage", //The name is always EntityImage
                                RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
                                DisplayName = new Label("Image", 1033),
                                Description = new Label("An image to represent the supplier.", 1033)

                            }
                        };

                        _serviceProxy.Execute(createEntityImageRequest);
                     
                        RetrieveEntityRequest retrieveSupplierEntityRequest = new RetrieveEntityRequest
                        {
                            EntityFilters = EntityFilters.Entity,
                            LogicalName = _customEntityName
                        };
                        RetrieveEntityResponse retrieveSupplierEntityResponse = (RetrieveEntityResponse)_serviceProxy.Execute(retrieveSupplierEntityRequest);
                     
                        EntityMetadata SupplierEntity = retrieveSupplierEntityResponse.EntityMetadata;

                        SupplierEntity.IsMailMergeEnabled = new BooleanManagedProperty(false);
            
                        UpdateEntityRequest updateSupplierRequest = new UpdateEntityRequest
                        {
                            Entity = SupplierEntity,
                            HasNotes = true
                        };



                        _serviceProxy.Execute(updateSupplierRequest);
                        //</snippetCreateUpdateEntityMetadata8>
                        //</snippetCreateUpdateEntityMetadata9>

                        Console.WriteLine("The Supplier entity has been updated");


                       
                        UpdateEntityForm(_customEntityName);

                        PublishAllXmlRequest publishRequest = new PublishAllXmlRequest();
                        _serviceProxy.Execute(publishRequest);
                   
                        Console.WriteLine("Customizations were published.");

                        //Provides option to view the entity in the default solution

                    }
                }
     
                catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
                {
                    // You can handle an exception here or pass it back to the calling method.
                    throw;
                }
            }

            public void UpdateEntityForm(String entityLogicalName)
            {

                QueryExpression qe = new QueryExpression("systemform");
                qe.Criteria.AddCondition("type", ConditionOperator.Equal, 2); //main form
                qe.Criteria.AddCondition("objecttypecode", ConditionOperator.Equal, entityLogicalName); //for new_bankaccount entity
                qe.ColumnSet.AddColumn("formxml");
                //Retrieve the first main entity form for this entity
                SystemForm bankAccountMainForm = (SystemForm)_serviceProxy.RetrieveMultiple(qe).Entities[0];

                XDocument bankAccountFormXml = XDocument.Parse(bankAccountMainForm.FormXml);
                //Set the showImage attribute so the entity image will be displayed
                bankAccountFormXml.Root.SetAttributeValue("showImage", true);

                // Definition of a custom tab containing the custom attributes created in this sample
                String formTabXmlString = @"<tab name=""custom_attributes""
           id=""{824792c4-ea85-1504-40e2-b780b26dc6ab}""
           IsUserDefined=""0""
           locklevel=""0""
           showlabel=""true""
           expanded=""true"">
       <labels>
        <label description=""Custom Attributes""
               languagecode=""1033"" />
       </labels>
       <columns>
        <column width=""100%"">
         <sections>
          <section name=""custom_section""
                   showlabel=""false""
                   showbar=""false""
                   locklevel=""0""
                   id=""{9bda81ec-e41b-7a4d-08b4-4536c5862ce1}""
                   IsUserDefined=""0""
                   layout=""varwidth""
                   columns=""1""
                   labelwidth=""115""
                   celllabelalignment=""Left""
                   celllabelposition=""Left"">
           <labels>
            <label description=""Section""
                   languagecode=""1033"" />
           </labels>
           <rows>
            <row>
             <cell id=""{04b95931-78c7-6913-a005-922d20e521b7}""
                   showlabel=""true""
                   locklevel=""0"">
              <labels>
               <label description=""Supplier Owner""
                      languagecode=""1033"" />
              </labels>
              <control id=""new_parent_contactid""
                       classid=""{270BD3DB-D9AF-4782-9025-509E298DEC0A}""
                       datafieldname=""new_parent_contactid""
                       disabled=""false"" />
             </cell>
            </row>
            <row>
             <cell id=""{6e5975d3-64b5-14eb-00e4-064e3dd298b9}""
                   showlabel=""true""
                   locklevel=""0"">
              <labels>
               <label description=""Supplier Name""
                      languagecode=""1033"" />
              </labels>
              <control id=""new_studentname""
                       classid=""{4273EDBD-AC1D-40d3-9FB2-095C621B552D}""
                       datafieldname=""new_suppliername""
                       disabled=""false"" />
             </cell>
            </row>
           
            <row>
             <cell id=""{0e9a3d8a-bd69-71ad-4ca4-2dcd10858719}""
                   showlabel=""true""
                   locklevel=""0"">
              <labels>
               <label description=""Supplier Number""
                      languagecode=""1033"" />
              </labels>
              <control id=""new_suppliernumber""
                       classid=""{5B773807-9FB2-42db-97C3-7A91EFF8ADFF}""
                       datafieldname=""new_suppliernumber""
                       disabled=""false"" />
             </cell>
            </row>
           </rows>
          </section>
         </sections>
        </column>
       </columns>
      </tab>";

                XDocument formTabXml = XDocument.Parse(formTabXmlString);

                //Adding this tab to the tabs element
                bankAccountFormXml.Root.Element("tabs").Add(formTabXml.Root);
                //Updateing the entity form definition
                bankAccountMainForm.FormXml = bankAccountFormXml.ToString();
                //saving the bank account form
                _serviceProxy.Update(bankAccountMainForm);

            }

    Wednesday, August 5, 2015 5:54 AM

Answers

  • Hi Nikkishah,

    Yes you are right, the problem with the "Classid"

    the Classid - represents the Unique key for the Type. The value which you used is for "DateTime" Control.

    Please refer this section.


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    • Proposed as answer by san Sanz Wednesday, August 5, 2015 7:57 AM
    • Marked as answer by NikkiShah Wednesday, August 5, 2015 10:26 AM
    Wednesday, August 5, 2015 7:57 AM
  • HI,

    CellID - should be unique, Please refer this section.  Before adding the cell, check whether that form is having any cells with same id or not if exisits use (Guid.NewGuid()) to get the new guid and use in the cell.


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    • Marked as answer by NikkiShah Wednesday, August 5, 2015 10:25 AM
    Wednesday, August 5, 2015 9:18 AM

All replies

  • Hi Nikkishah,

    As per your code , you are doing following actions

    1 . Creating an Entity - "new_supplier"

    2. Creating an attributs- "new_suppliername","new_suppliernumber","Image attribute"

    3. Created a relation (1:N) - with contact

    4. Updating the form - with custom attributes,

    As per my understanding - the control you placed is causing issue -

        <control id=""new_parent_contactid""
                        classid=""{270BD3DB-D9AF-4782-9025-509E298DEC0A}""
                        datafieldname=""new_parent_contactid""
                        disabled=""false"" />


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    • Proposed as answer by san Sanz Wednesday, August 5, 2015 7:00 AM
    Wednesday, August 5, 2015 6:32 AM
  • yes  i am doing this actions .

    no when I am putting code of supplier number then getting error..

       <row>
             <cell id=""{0e9a3d8a-bd69-71ad-4ca4-2dcd10858719}""
                   showlabel=""true""
                   locklevel=""0"">
              <labels>
               <label description=""Supplier Number""
                      languagecode=""1033"" />
              </labels>
              <control id=""new_suppliernumber""
                       classid=""{5B773807-9FB2-42db-97C3-7A91EFF8ADFF}""
                       datafieldname=""new_suppliernumber""
                       disabled=""false"" />
             </cell>
            </row>

    can you tell me what is cell id and class id?that is different for all the attribute or same for same type of attribute?

    Wednesday, August 5, 2015 6:36 AM
  • Hi Nikkishah,

    Yes you are right, the problem with the "Classid"

    the Classid - represents the Unique key for the Type. The value which you used is for "DateTime" Control.

    Please refer this section.


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    • Proposed as answer by san Sanz Wednesday, August 5, 2015 7:57 AM
    • Marked as answer by NikkiShah Wednesday, August 5, 2015 10:26 AM
    Wednesday, August 5, 2015 7:57 AM
  • Thank you .

    I need to hcange in cell id?..what is cell id?

    Wednesday, August 5, 2015 8:08 AM
  • Yes, it should be - "{4273EDBD-AC1D-40D3-9FB2-095C621B552D}" .

    Please refer this section.


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    Wednesday, August 5, 2015 8:41 AM
  • No,  "{4273EDBD-AC1D-40D3-9FB2-095C621B552D}" is the class id  ,

    I am talking about cell id   <cell id=""{0e9a3d8a-bd69-71ad-4ca4-2dcd10858719}"" ...>

    what's this ,Do I need to change ? how would i know whethter is correct or not?

    Wednesday, August 5, 2015 8:46 AM
  • just out of curiosity, why you need to create dynamically an entity and its form?

    My blog: www.crmanswers.net - CRM Theme Generator

    Wednesday, August 5, 2015 8:46 AM
  • Hi,

     I want to create purchase order from sales order. so when user click on purchase order button then i have to create purchase order and want to save it to purchase order entity. How can I achieve this.. is there any better way to achieve this?

    Wednesday, August 5, 2015 8:49 AM
  • Hi NikkiShah,

    I suggest to attend a CRM Customization course instead of trying without knowing how CRM customization works.

    If you can't attend a course, on youtube there are several videos, there are also free resources like http://crmbook.powerobjects.com/

    In addition if you have a PartnerSource or a CustomerSource account you can watch the course at http://itacademy.microsoft.com/

    At Amazon or other resellers you can find several books or e-books regarding Dynamics CRM, PacktPub also has books regardings CRM and often they do discounts (like 5$/e-book)

    Hope it helps


    My blog: www.crmanswers.net - CRM Theme Generator

    Wednesday, August 5, 2015 9:05 AM
  • HI,

    CellID - should be unique, Please refer this section.  Before adding the cell, check whether that form is having any cells with same id or not if exisits use (Guid.NewGuid()) to get the new guid and use in the cell.


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    • Marked as answer by NikkiShah Wednesday, August 5, 2015 10:25 AM
    Wednesday, August 5, 2015 9:18 AM
  • Thank you Guido,

    I will study ,thank you for your valuable suggestions.

    But ask one think,can u give my one answer -Is it possible to create new entity using javascript?if yes,then please give some reference link once.

    Thursday, August 6, 2015 5:53 AM