Random Musings

July 6, 2009

Create simple domain generator by reading from CRM Metadata

Filed under: c#,Dynamics CRM — haditeo @ 9:30 pm
Tags: ,

Objective
I would like to create my own domain (class) from the custom CRM entity. The advantage of this is to simplify domain persistence and retrieval between application layer and database layer (CRM Web Service)

How to achieve this ?

  • First of all we utilize RetrieveEntityRequest message to retrieve all attributes of the specific entity.
                    RetrieveEntityRequest request = new RetrieveEntityRequest();
                    request.LogicalName = entityName;
                    request.EntityItems = EntityItems.IncludeAttributes;
    
                    RetrieveEntityResponse response = (RetrieveEntityResponse)metadataService.Execute(request);
    
                    EntityMetadata entityMetadata = response.EntityMetadata;
    
                    foreach (AttributeMetadata attributeMetadata in entityMetadata.Attributes)
                    {
                        //loop through all attributes here.
                    }
    
  • There are some interesting attributes of the AttributeMetadata. Such as LogicalName attribute, DisplayName attribute and AttributeType attribute
  • By looping through each of the attributes, i read the relevant template file consisting of the code templates and replace the necessary placeholder with the actual values

    private LookupProperty _#internalVariableName#;
    [CrmAttributeMapping("#crmAttributeMapping#")]
    public LookupProperty #publicPropertyName#
    {
        get { return _#internalVariableName#; }
        set { _#internalVariableName# = value; }
    }
    
    public void Set#publicPropertyName#(Guid value)
    {
        _#internalVariableName# = new LookupProperty("#crmAttributeMapping#", new Lookup("#lookupAttributeMapping#", value));
    }
    
    public bool Is#publicPropertyName#Null()
    {
        if (_#internalVariableName# == null)
            return true;
    
        if (_#internalVariableName#.Value == null)
            return true;
    
        if (_#internalVariableName#.Value.Value == Guid.Empty)
            return true;
            
        return false;
    }
    

    Here are the result of the placeholder value replacement

    private LookupProperty _owninguser;
    [CrmAttributeMapping("owninguser")]
    public LookupProperty owninguser
    {
        get { return _owninguser; }
        set { _owninguser = value; }
    }
    
    public void Setowninguser(Guid value)
    {
        _owninguser = new LookupProperty("owninguser", new Lookup("#lookupAttributeMapping#", value));
    }
    
    public bool IsowninguserNull()
    {
        if (_owninguser == null)
            return true;
    
        if (_owninguser.Value == null)
            return true;
    
        if (_owninguser.Value.Value == Guid.Empty)
            return true;
            
        return false;
    }
    
  • Some clean up needs to be performed to tidy up the public Property name, but this code generator has helped me tremendeously to eliminate boring and repititive tasks of creating custom domain objects by hand.

July 4, 2009

Json.NET and json.js

Filed under: c# — haditeo @ 11:45 am
Tags: ,

Let’s say i have an “Argument” class and “Customer” class below :

    public class Argument
    {
        private string _filterComboType;

        public string FilterComboType
        {
            get { return _filterComboType; }
            set { _filterComboType = value; }
        }

        private string _selectedNameClientId;
        
        public string SelectedNameClientId
        {
            get {return _selectedNameClientId;}
            set {_selectedNameClientId = value;}
        }

        private List<Customer> customerList;

        public List<Customer> Customer
        {
            get { return customerList; }
            set { customerList = value; }
        }
        
        public Argument()
        {
        
        }
    }
    public class Customer
    {
        private string _customerType;
        private string _customerText;
        private string _customerValue;

        public string ObjectType
        {
            get { return _customerType; }
            set { _customerType = value; }
        }

        public string CustomerText
        {
            get { return _customerText; }
            set { _customerText = value; }
        }

        public string CustomerValue
        {
            get { return _customerValue; }
            set { _customerValue = value; }
        }

        public Customer(string strCustomerType, string strCustomerText, string strCustomerValue)
        {
            _customerType = strCustomerType;
            _customerText = strCustomerText;
            _customerValue = strCustomerValue;
        }
        
        public Customer()
        {
        
        }
    }

The purpose of these objects is to be passed along from the “main” page to the “lookup” page to be processed further.

Refer to the sample result of these objects on the JSON string format for one Customer

{"FilterComboType":"NameOfClient","SelectedNameClientId":null,"Customer":[{"ObjectType":"account","CustomerText":"Account E","CustomerValue":"c0b377cf-1766-de11-8f5c-0003ffb4d763"}]}

and here is the sample JSON string result for multiple Customers

{"FilterComboType":"ContactList","SelectedNameClientId":null,"Customer":[{"ObjectType":"contact","CustomerText":"Lim","CustomerValue":"e0203ac0-2a3d-de11-8641-0003ffc4d675"},{"ObjectType":"contact","CustomerText":"Contact B","CustomerValue":"a0ff10e6-1766-de11-8f5c-0003ffb4d763"},{"ObjectType":"contact","CustomerText":"Contact C","CustomerValue":"9088e0ec-1766-de11-8f5c-0003ffb4d763"}]}

What are the advantages of creating JSON string format ?

you can access those objects at the client side easily, in example :

function AccessArgument(arg)
{
  var jsonStringArgument = arg;
  var argument = eval(jsonStringArgument);

  var filterComboTypeValue = argument.FilterComboType;
  
  for(var i=0; i<argument.Customer.length; i++)
  {
    var customerValue = argument.Customer&#91;i&#93;.CustomerValue;
  }
}

&#91;/sourcecode&#93;

Notice how easily i'm accessing a list of the argument's Customer in array object at the client side

How do you convert from JSON string to the actual object itself ?

&#91;sourcecode='jscript'&#93;
var objectArgument = eval('a Json String');
&#91;/sourcecode&#93;

How do you convert from actual object back to the JSON string ? You need <a href="http://www.json.org/js.html">json2.js</a> which can be downloaded <a href="http://www.json.org/js.html">here</a> . The javascript file needs to be included in the html head.


var jsonText = JSON.stringify(objectArgument);

I like to use Newtonsoft JSON.NET to serialize my C# object to the JSON string format as well as deserialize my C# object back to the JSON string format

Here are the code sample how to serialize the Argument object into JSON string

txtAccountListJson.Text = Newtonsoft.Json.JavaScriptConvert.SerializeObject(newArg);

And here are the code sample in how to serialize the JSON string back to the actual object

Argument arg = (Argument)Newtonsoft.Json.JavaScriptConvert.DeserializeObject(txtContactListJson.Text, typeof(Argument));

Create a free website or blog at WordPress.com.