How to Query Entity in Dynamics CRM Portal - AdxStudio(Using Odata method and Liquid Template Method)

There are bascially 2 ways we can query entity in CRM Customer portal:

Method 1: Via Liquid Template
Method 2: Via Odata

In this blog I am going to show how we can query crm entity using above 2 methods one by one.

Mthod 1: Using Liquid Template
Follow the below steps for creating code:
Step1 : Create a new Web Template where you'll have to add your fetch xml code. [? A Web Template includes the actual coded design on the record itself.  Because of this, the code can be accessed from the UI or even programmatically via SDK or API.]

A liquid code contains below tags:
Tags – Liquid tags are the programming logic that tells templates what to do. Tags are wrapped in: {% %} characters.   Some tags can take parameters.  Examples: if unless else elsif case and or for cycle tablerow include assign increment.

Objects – Liquid objects contain attributes to output dynamic content on the page. For example, the page object contains an attribute called title that can be used to output the title of a page.

Filters – Filters are simple methods that modify the output of numbers, strings, variables and objects.  Some examples are.. concat, first, last, order_by, size, skip, where, minus, plus, modulo, upcase, downcase, truncate.

Filters:
Add below sample code inside source field of web template:


 {% assign contactid = request.params['contactid'] %}  
 {% fetchxml portalQuery %}  
 <fetch>  
  <entity name="contact">  
   <attribute name="contactid" /> 
   <attribute name="statuscode" /> 
   <attribute name="new_name" />  
   <filter type="and">  
    <condition attribute="contactid" operator="eq" value="{{contactid}}" />  
   </filter>  
  </entity>  
 </fetch>  
 {% endfetchxml %}  
 {% assign result = portalQuery.results.entities %}  
 [  
 {% for item in result %}  
  {   
   "statuscode" : "{{item.statuscode.Value}}",
   "new_name" : "{{item.new_name}}"   
  }  
 {% if item != result.last %},{% endif %}  
 {% endfor %}  
 ]   



Step 2: Create a Page Template, select type as web template and add your web template in Web template column. [? A Page Template is just a pointer to where the Template is stored]
Step 3: Create a Web Page and add above created Page template.

So following steps you have bascially created a new page and you have provided code in web template to fetch data from crm which you can use in your JQuery code to fetch data as per your requirement:


var ContactId = $('#contactid').val();

 function GetContactRecord(contactid) {  
   var urls = "/contactdetails/?contactid=" + contactid;  
   var idresult = "";  
   var result = "";  
   $.ajax({  
     type: "Get",  
     contentType: "application/json; charset=utf-8",  
     datatype: "json",  
     url: urls,  
     beforeSend: function (XMLHttpRequest) {  
       //Specifying this header ensures that the results will be returned as JSON.  
       XMLHttpRequest.setRequestHeader("Accept", "application/json");  
     },  
     success: function (data, textStatus, XmlHttpRequest) {  
       name = data[0].name;  
       if ((name !== "" && name !== undefined)) {  
         $("#new_name").val(name);  
       }  
     },  
     error: function (XMLHttpRequest, textStatus, errorThrown) {  
       alert(errorThrown);  
       errorHandler(XMLHttpRequest, textStatus, errorThrown);  
     }  
   });  
 }  



Method 2: Via OData Method

Step 1: Create an Entity list of entity and enable OData
Step 2: While enabling OData, select view which has list of all columns which you want to fetch for your logic
Step 3: Once above 2 steps done, add below code and make changes according to your requirement

$(document).ready(function () {
    debugger;
    var ContactId = $('#contactid').val();
    var filteroption = "contactid eq guid'" + ContactId + "'";
    var odataUri = $(location).context.origin + "/_odata/Contacts";
    odataUri += "?$filter=" + encodeURIComponent(filteroption);

    $.ajax({
        type: 'GET',
        contentType: 'application/json; charset=utf-8',
        datatype: 'json',
        url: odataUri,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader('Accept', 'application/json');
        },
        success: function (data, textStatus, xhr) {
            var ContactName = data.value[0].fullname;
            $("#new_name").val(ContactName.Value);
        },
        error: function (xhr, textStatus, errorThrown) {
            alert(textStatus + ' ' + errorThrown);
        }
    });
});

Comments

Popular posts from this blog

How to show enlarge image when mouse hover on image or link in Asp.Net(c#) using JavaScript

StateCode and StatusCode Values for mostly used entities in Microsoft Dynamics CRM 2013

Search data in Gridview on Textbox Key press event using JQuery in Asp.Net- C#

go to top image