locked
Problem with dynamic entity RRS feed

  • Question

  • Hi,

    I've got this code to clone a custom entity called "new_prop"

    DynamicEntity oldProp = (DynamicEntity)crmservice.Retrieve("new_prop", new Guid(Request.QueryString["id"]), new AllColumns());
    DynamicEntity newProp = oldProp;
    newProp.Properties["new_propid"] = null;
    newProp.Properties["new_name"] = string.Format("Clone of: {0}", newProp.Properties["new_name"]);
    Guid newPropId = crmservice.Create(newProp);
    Response.Write(newPropId.ToString());
    

    Its giving me this error and i'm not sure how to solve it.

    There is an error in XML document (1, 296). System.Xml at System.Xml.Serialization..XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)

    Any help appreciated

    Thanks

    Thursday, January 20, 2011 12:35 PM

Answers

  • Yes that is your issue... you need to treat dynamic entities differently in your retrieves. Here is another example, substitute your GUID in where I put mine to test.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;
    using Microsoft.Crm.Sdk.Query;
    
    namespace CreateDynamicEntity
    {
     class Program
     {
      static void Main(string[] args)
      {
       CrmService c = new CrmService()
       {
        Url = @"http://crmdev1/mscrmservices/2007/crmservice.asmx",
        Credentials = System.Net.CredentialCache.DefaultNetworkCredentials,
        CrmAuthenticationTokenValue = new CrmAuthenticationToken() { AuthenticationType = 0, OrganizationName = "crmdev1" }
       };
       TargetRetrieveDynamic tr = new TargetRetrieveDynamic() { EntityId = new Guid("8E2A4FA5-F524-E011-AC5D-005056A8000D"), EntityName = "contact" };
       RetrieveRequest rr = new RetrieveRequest() { ReturnDynamicEntities = true, Target=tr, ColumnSet= new AllColumns() };
       DynamicEntity contact = (DynamicEntity)((RetrieveResponse)c.Execute(rr)).BusinessEntity;
       Console.WriteLine(contact.Properties.Count);
      }
     }
    }
    
    Friday, January 21, 2011 7:35 PM

All replies

  • You cannot create a dynamic entity using Create method .

    You have to use targetCreateDynamic class and use the execute method to do this.

    Thursday, January 20, 2011 12:51 PM
  • Thanks for that - I added this...

    // Create the target.
    TargetCreateDynamic targetCreate = new TargetCreateDynamic();
    targetCreate.Entity = newProp;
    // Create the request object.
    CreateRequest create = new CreateRequest();
    // Set the properties of the request object.
    create.Target = targetCreate;
    // Execute the request.
    CreateResponse created = (CreateResponse)service.Execute(create);

    But I get this exception:

    'Microsoft.Crm.SdkTypeProxy.service' does not contain a definition for 'Execute'

    Thanks

    Thursday, January 20, 2011 1:30 PM
  • rename you "service" to crmService
    Oleksandr Klymenko,
    My Blog: www.darkaxe.wordpress.com
    Thursday, January 20, 2011 1:37 PM
  • Still got same XML error - here is code:

    DynamicEntity oldProp = (DynamicEntity)crmservice.Retrieve("new_prop", new Guid(Request.QueryString["id"]), new AllColumns());
    DynamicEntity newProp = oldProp;
    newProp.Properties["new_propid"] = null;
    newProp.Properties["new_name"] = string.Format("Clone of: {0}", newProp.Properties["new_name"]);
    // Create the target.
    TargetCreateDynamic targetCreate = new TargetCreateDynamic();
    targetCreate.Entity = newProp;
    // Create the request object.
    CreateRequest create = new CreateRequest();
    // Set the properties of the request object.
    create.Target = targetCreate;
    // Execute the request.
    CreateResponse created = (CreateResponse)crmservice.Execute(create);
    Response.Write(created.id.ToString());

    Thanks

     

    Thursday, January 20, 2011 2:07 PM
  • Hi

    Try to remove the new_propid property instead of setting it to null.

    newProp.Properties.Remove("new_propid");
    

    /Jonas

    Thursday, January 20, 2011 2:29 PM
  • Still same unfortunately...
    Thursday, January 20, 2011 2:36 PM
  • :(

    How do you instantiate your service?

    Thursday, January 20, 2011 2:49 PM
  • Here is the complete sample. Thanks

       try
              {
                using (new CrmImpersonator())
                {
                  CrmAuthenticationToken token = CrmAuthenticationToken.ExtractCrmAuthenticationToken(Context, orgname);
    
                  token.OrganizationName = "MyOrg";
                  token.AuthenticationType = 0;
    
                  CrmService crmservice = new CrmService();
    
                  //crmservice.UseDefaultCredentials = true;
                  crmservice.Credentials = System.Net.CredentialCache.DefaultCredentials;
                  crmservice.Url = "http://server/MSCRMServices/2007/crmservice.asmx";
                  crmservice.CrmAuthenticationTokenValue = token;
                                
                  DynamicEntity oldProp = (DynamicEntity)crmservice.Retrieve("new_prop", new Guid(Request.QueryString["id"]), new AllColumns());
                  DynamicEntity newProp = oldProp;
                  
                  //newProp.Properties["new_propid"] = null;
                  newProp.Properties.Remove("new_propid");
    
    
                  newProp.Properties["new_name"] = string.Format("Clone of: {0}", newProp.Properties["new_name"]);
    
                  // Create the target.
                  TargetCreateDynamic targetCreate = new TargetCreateDynamic();
                  targetCreate.Entity = newProp;
                  // Create the request object.
                  CreateRequest create = new CreateRequest();
                  // Set the properties of the request object.
                  create.Target = targetCreate;
                  // Execute the request.
                  CreateResponse created = (CreateResponse)crmservice.Execute(create);
                                
                  Response.Write(created.id.ToString());
    
    
                  //HEADER
                  //quote oldQuote = (quote)crmservice.Retrieve(EntityName.quote.ToString(), new Guid(Request.QueryString["id"]), new AllColumns());
                  //quote newQuote = oldQuote;
                  //newQuote.quoteid = null;
                  //newQuote.name = string.Format("Clone of: {0}", newQuote.name);
                  //Guid newQuoteId = crmservice.Create(newQuote);
                  //QUOTE PRODUCTS
                  //QueryByAttribute query = new QueryByAttribute();
                  //query.EntityName = EntityName.quotedetail.ToString();
                  //query.Attributes = new string[] { "quoteid" };
                  //query.Values = new object[] { new Guid(Request.QueryString["id"]) };
                  //query.ColumnSet = new AllColumns();
    
                  //BusinessEntityCollection lineItems = crmservice.RetrieveMultiple(query);
    
                  //foreach (quotedetail lineItem in lineItems.BusinessEntities)
                  //{
                    //lineItem.quotedetailid = null;
                    //Lookup l = new Lookup();
                    //l.name = EntityName.quote.ToString();
                    //l.Value = newQuoteId;
                    //lineItem.quoteid = l;
                    //crmservice.Create(lineItem);
                  //}
    
    
    
                }
              }
              catch (Exception ex)
              {
                Response.Write(ex.Message + " " + ex.Source + " " + ex.StackTrace);
              }
    

    Thursday, January 20, 2011 2:59 PM
  • You cannot create a dynamic entity using Create method .

    You have to use targetCreateDynamic class and use the execute method to do this.

    I am afraid that this was a wrong statement. You can definitely use Create method to create a record for dynamic entity...
    Daniel Cai | http://danielcai.blogspot.com
    Thursday, January 20, 2011 3:29 PM
  • Could anyone provide a working example of creating a dynamic entity. Really struggling to find the problem :(

    Thanks a lot

    Thursday, January 20, 2011 11:59 PM
  • Here is a "Hello World" example that should get you started.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;
    
    namespace CreateDynamicEntity
    {
      class Program
      {
        static void Main(string[] args)
        {
          CrmService c = new CrmService()
          {
            Url = @"http://crmdev1/mscrmservices/2007/crmservice.asmx",
            Credentials = System.Net.CredentialCache.DefaultNetworkCredentials,
            CrmAuthenticationTokenValue = new CrmAuthenticationToken() { AuthenticationType = 0, OrganizationName = "crmdev1" }
          };
    
          DynamicEntity contact = new DynamicEntity() { Name = "contact" };
          contact.Properties.Add(new StringProperty() { Name = "lastname", Value = "MSCRM Forums alex test" });
    
          c.Create(contact);
        }
      }
    }
    
    We are assuming active directory here, and that your default org is set appropriately.
    Friday, January 21, 2011 12:31 AM
  • That works perfectly thanks.

    The problem it seems is with retrieving the old one.

    DynamicEntity oldProp = (DynamicEntity)crmservice.Retrieve("new_prop", new Guid(Request.QueryString["id"]), new AllColumns());

    Do you know what the problem is with that line?

    Thanks

     

    Friday, January 21, 2011 2:09 PM
  • Yes that is your issue... you need to treat dynamic entities differently in your retrieves. Here is another example, substitute your GUID in where I put mine to test.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;
    using Microsoft.Crm.Sdk.Query;
    
    namespace CreateDynamicEntity
    {
     class Program
     {
      static void Main(string[] args)
      {
       CrmService c = new CrmService()
       {
        Url = @"http://crmdev1/mscrmservices/2007/crmservice.asmx",
        Credentials = System.Net.CredentialCache.DefaultNetworkCredentials,
        CrmAuthenticationTokenValue = new CrmAuthenticationToken() { AuthenticationType = 0, OrganizationName = "crmdev1" }
       };
       TargetRetrieveDynamic tr = new TargetRetrieveDynamic() { EntityId = new Guid("8E2A4FA5-F524-E011-AC5D-005056A8000D"), EntityName = "contact" };
       RetrieveRequest rr = new RetrieveRequest() { ReturnDynamicEntities = true, Target=tr, ColumnSet= new AllColumns() };
       DynamicEntity contact = (DynamicEntity)((RetrieveResponse)c.Execute(rr)).BusinessEntity;
       Console.WriteLine(contact.Properties.Count);
      }
     }
    }
    
    Friday, January 21, 2011 7:35 PM