Tuesday, October 27, 2015

Add CustomView Dynamically to a lookup control Using Javascript in MSCRM

Code Snippet:

 var viewId = "{00000000-0000-0000-0000-000000000001}";
 var entName = "new_user";
 var vwName = "Job Resources";
 var resFetch = "<fetch version='1.0' output-format='xml-platform' mapping='logical' >" +
                           "<entity name='new_user'>" +
                           "<attribute name='mtctb_name' />" +
                           "<attribute name='createdon' />" +
                           "<order attribute='mtctb_name' descending='false' />" +
                           "</filter>" +
                           "</entity>" +
                           "</fetch>";

Problem with FetchXml Generation , Go through the link
http://a2zcrmstuff.blogspot.in/2015/10/how-to-generate-fetchxml-using-advanced.html

var resLayout = "<grid name='resultset' object='10030' jump='mtctb_name' select='1' icon='1' preview='1'>" +
                    "<row name='result' id='new_user'>" +
                    "<cell name='mtctb_name' width='200'/>" +
                    "<cell name='mtctb_user' width='100'/>" +
                    "<cell name='statecode' width='100'/>" +
                    "<cell name='createdon' width='125'/>" +
                    "</row>" +
                    "</grid>";

Problem with LayoutXml Generation , Go through the link
http://a2zcrmstuff.blogspot.in/2015/10/how-to-generate-layoutxml-using.html

    Xrm.Page.getControl("FieldName").addCustomView(viewId, entName, vwName, resFetch, resLayout, true);

   To Disable View Picker Go Through this snippet:

       For 2013 CRM
              $("#lookupname").find("img").attr("disableviewpicker","1");
       For 2015 CRM
               document.getElementById("lookupname").attributes.disableviewpicker.value = "1";
                                                                              (or)
               parent.document.getElementById("lookupname").attributes.disableviewpicker.value = "1";


If you need to check for CRM Versions, go through this snippet:

     var APPVersion = "";
     try{
          APPVersion = APPLICATION_VERSION;
     }
     catch (e) {
          APPVersion = parent.APPLICATION_VERSION;
     }

     if(AppVersion < "7.0"){
          This is an 2013 CRM
     }
     else{
         This is an 2015 CRM
     }

Description:

  • To add a custom view to a lookup control you need to have (FetchXml,LayoutXml,ViewId,ViewName,EntityName)
  • Generate FetchXml and LayoutXml from Advanced Find,ViewName is your choice,ViewId should be unique so i have given statically which is unique,Provide your enity name and finally true is nothing but defaultview,If you need to set the custom view as default then set it to true else if you not needed then go for false.
Syntax:

Xrm.Page.getControl("FieldName").addCustomView(ViewId,Entityname,ViewName,FetchXml,LayoutXml,DefaultView(true,false));

If you need to disable view picker then go for this line of code:

$("#FieldName").find("img").attr("disableviewpicker", "1"); //Jquery reference is required(Works only in on-premesis CRM)

1 comment:

  1. Thank You sir, it extended me help and gave me idea to achieve my req....

    ReplyDelete