Retrieve the data using Odata Query in D365 Portal / ADX Studio Portal.

Hello Guys.....With the help of Odata Query we can retrieve the data in Portal from CRM and on the basis of this data we can apply some validation on it.
Here i will show you, How to retrieve the data from CASE entity using Odata query.

Before Moving forward, if you don't know, how to apply client side validation then click Here to know that.

Step 1: Go to Portal > Entity List. Create a new Entity List for Case Entity.



Step 2: Now scroll down this record and go to Odata Feed Section.
  • Click the Enabled Checkbox to enable the odata.
  • Give the Entity Type Name and Entity Set Name.
  • Select the view from which you want to retrieve the data or you can set your own custom view.

Step 3: Now, To retrieve the data, Hit the URL in browser.
  • URL : Portal_URL/_odata : This URL give the list of all entity set for which you have enabled the Odata.
  • URL : Portal_URL/_odata/Entity_Set_Name : This URL retrieve the data 



How to use odata to apply validation:

Here I applied validation on case like user not able to create case with same Title.  Check the below code and you can change this code as your requirement.


if (window.jQuery) {
   (function ($) {
      $(document).ready(function () {
         if (typeof (Page_Validators) == 'undefined') return;
         // Create new validator
         var newValidator = document.createElement('span');
         newValidator.style.display = "none";
         newValidator.id = "titleValidator";
         newValidator.controltovalidate = "title";
         newValidator.errormessage = "<a href='#title_label'>This Case has been already generated.</a>";
         newValidator.validationGroup = ""; // Set this if you have set ValidationGroup on the form
         newValidator.initialvalue = "";
         newValidator.evaluationfunction = function () {
       
var count =   GetCase();
            if (count > 0 ) 
              return false;   // Return false mean apply validation.
             else 
              return true;   // Return true mean successful.         
         };
         // Add the new validator to the page validators array:
         Page_Validators.push(newValidator);
         // Wire-up the click event handler of the validation summary link
         $("a[href='#title_label']").on("click", function () { scrollToAndFocus('title_label','title'); });
      });
   }(window.jQuery));
}

function GetCase(){
  var count = 0;
  var title=$("#title").val();

$.ajax({
    type: "GET",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: "~/_odata/Cases?$filter=title eq '"+title+"'",
    beforeSend: function(XMLHttpRequest) {
        XMLHttpRequest.setRequestHeader("Accept", "application/json");
    },
    async: false,
    success: function(data, textStatus, xhr) {
     count = data.value.length;        
    },
    error: function(xhr, textStatus, errorThrown) {
        Xrm.Utility.alertDialog(textStatus + " " + errorThrown);
    }
  
});

return count;
}


Note: Refer the below link for complex Odata Query.
    https://msdn.microsoft.com/en-us/library/gg309461(v=crm.7).aspx



Click Here to know, Cache Problem In D365 Portal / ADX Studio Portal.