For that first you have to get field list of any entity from CRM using “RetrieveEntityResponse” method of CRM SDK. You can easily get code for this from internet.
Now create a view named “AccountFilter” and copy and paste following code to that page. It is full javascript code which is useful to control behavior like hide-show on specific option, change control on field type like optionset, text, Boolean etc.
When you run the application, it will show the page like below with the dropdown of field.
When you select text field from field list, the java script function “manageControl” automatically display a text box on right side as below image.
When you select OptionSet field from field list, the java script function “manageControl” calls the method “GetOptionList” on Search Controller and get the list of that OptionSet and set to right dropdown as below.
When you select lookup field from field list, the function “manageControl” call the method “GetLookup” on Search Controller and which get the list of contacts and set to right dropdown same as OptionSet.
The GetOptionList and GetLookup ActionResult methods are as below.
You can see that the GetLookup calls the methods “GetLookipItems_of_Account” which get the list of Active Contact with GuId and Name. Code for this method is as below.
When you click on Add filter button after selecting field and their filter value the page shows like this.
In above picture, you can see a dropdown with “AND” and “OR” options, which is useful to add logical operator to filter query. This dropdown only shows after adding first filter to query. You can see that first time when you load application it will not display but it shows after added first filter.
Following image elaborate multiple filter options.
The above condition creates query like
(AccountName startwith A.) and (City = London or City = Liverpool).
The middle dropdown contains option like “Equals”, “Contains” etc. there is a provision with selection of these options the visibility of last div depends. When you select “Equals” or “Contains” or “Start With” the last div for selecting value to filter will display but when you select option like “Contains Data” of “No Data” the last div will not display. The behavior of this is same like CRM Advanced search window.
Finally, create a method which filters account records based on condition using QueryExpression as something like below code. The “Submit” button on above picture will call the following method. Note : the below code is only filter records with AND logical operator, you have to manage and update below method to filter records with “AND” and “OR”. When you use OR logical operator at that time you have to manage code to get the value of previous line and current line and then add with logical OR operator to query expression. I remain this part for you in below code.
This is not useful for beginners in CRM and MVC with java script, because there are some other codes related to CRM connection, retrieve records, retrieve Optionset list which have to add to get proper work around same as CRM Advanced search. This is only java script part to manage control behavior as per Selected Field
This article has been shared by Ethan Millar working with Aegis SoftTech as senior developer from last five years. He has extensive experience in .Net development, different Java technologies and Frameworks. The objective of writing this post is to discuss about the custom CRM search application in MVC. The conclusion has been drawn after practical research and implementation by Asp.Net MVC development team at Aegis SoftTech.