Pages

Friday, September 6, 2013

MS Dynamics CRM 2011 - Display Lists/Views Dynamically

Audit_matchlist.htm - HTML Page Code

This page will build fetchxml, display event list from event entity, matchtype option set, and bind the result in the frame.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
    <meta content="utf-8" http-equiv="encoding"/>
    <meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
    <meta http-equiv="CACHE-CONTROL" content="NO-CACHE"/>
    <link href="/WebResources/dns_CSS/audit.css" rel="Stylesheet" />

    <script src="../ClientGlobalContext.js.aspx" type="text/javascript"></script>
    <script src="/WebResources/dns_JavaScript/Jquery1.4.1.min.js" type="text/javascript" language="javascript"></script>
    <script src="/WebResources/dns_JavaScript/Jason.js" type="text/javascript" language="javascript"></script>
    <script src="/WebResources/dns_JavaScript/advancedfindview.js" type="text/javascript" language="javascript"></script>
    <script src="/WebResources/dns_JavaScript/DataOperations.js" type="text/javascript" language="javascript"></script>
    <script src="/WebResources/dns_JavaScript/audit.js" type="text/javascript" language="javascript"></script>
    <script type="text/javascript">
        var ECMRole = CheckUserRole("ECM");
       
        //alert(window.location.href);


        var fetchBaseXML = '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">';
            "fetchBaseXML += '    <"entity name="dns_match">';
            if (!ECMRole) {
                fetchBaseXML += '        <attribute name="dns_review" />';
            }
            fetchBaseXML += '        <attribute name="status" />';
            fetchBaseXML += '        <attribute name="matchid" />';
            fetchBaseXML += '        <order attribute="status" descending="false" />';
            fetchBaseXML += '        #ENTITY-FILTER#';
            fetchBaseXML += '        <link-entity name="contact" from="contactid" to="dns_matchuser" visible="false" alias="contact_alias">';
            fetchBaseXML += '            <attribute name="lastname" />';
            fetchBaseXML += '            <attribute name="fullname" />';
            fetchBaseXML += '            <attribute name="firstname" />';
            fetchBaseXML += '            #LINK-ENTITY-FILTER#';
            fetchBaseXML += '        </link-entity>';
            fetchBaseXML += '        <link-entity name="dns_book" from="dns_bookid" to="dns_matchbookuser" alias="ai">';
            fetchBaseXML += '            <link-entity name="dns_batch" from="dns_batchid" to="dns_batchid" alias="aj">';
            fetchBaseXML += '                <filter type="and">';
            fetchBaseXML += '                    <condition attribute="dns_eventid" operator="in">';
            fetchBaseXML += '                        #LINK-ENTITY-event-FILTER#';
            fetchBaseXML += '                    </condition>';
            fetchBaseXML += '               </filter>';
            fetchBaseXML += '            </link-entity>';
            fetchBaseXML += '        </link-entity>';
            fetchBaseXML += '    </entity>';
             fetchBaseXML += '</fetch>';

        var fetchLayoutXML = '<grid name="resultset" object="2" jump="dns_name" select="1" preview="1" icon="1">';
            fetchLayoutXML += '    <row name="result" id="dns_matchid">';
            if (!ECMRole) {
                fetchLayoutXML += '        <cell name="dns_review" width="100" />';
            }
            fetchLayoutXML += '        <cell name="status" width="100" />';
            fetchLayoutXML += '        <cell name="contact_alias.firstname" width="100" disableSorting="1" relatedentityname="contact" relatedentityattr="contactid" primaryentityattr="dns_matchuser" relationshipid="{963e65da-8b56-455c-a471-940f32615cb7}" relationshipname="dns_contact_dns_match_user" />';
            fetchLayoutXML += '        <cell name="contact_alias.lastname" width="100" disableSorting="1" relatedentityname="contact" relatedentityattr="contactid" primaryentityattr="dns_matchuser" relationshipid="{963e65da-8b56-455c-a471-940f32615cb7}" relationshipname="dns_contact_dns_match_user" />';
            fetchLayoutXML += '        <cell name="contact_alias.middlename" width="100" disableSorting="1" relatedentityname="contact" relatedentityattr="contactid" primaryentityattr="dns_matchuser" relationshipid="{963e65da-8b56-455c-a471-940f32615cb7}" relationshipname="dns_contact_dns_match_user" />';
            fetchLayoutXML += '    </row>';
            fetchLayoutXML += '</grid> ';


        function OnSearch() {
            document.getElementById("FetchIFrame").src = "about:blank"
            IFrameDisplayList();
        }

        function IFrameDisplayList() {
            window.book = new DisplayAdvanceFindView("FetchIFrame", "dns_match", "{0963E43B-D7F3-E211-B0EF-0050568C2EE0}", "dns_status:1;dns_matchuser:1");
            book.RegisterOnTab(0);
        }

        function getCRMFetchXML() {
            var fetchQuery = fetchBaseXML;
            var v_matchtype = document.getElementById("matchtype").value;
            var v_event_value = document.getElementById("event").value;
            var v_event_name = document.getElementById("event").name;
            var entityfilter = '';
            entityfilter += '<filter type="and">';
            if (v_matchtype != "0") {
                entityfilter += '    <condition attribute="dns_status" operator="eq" value="' + v_matchtype + '" />'
            }
          
            entityfilter += '    <condition attribute="dns_matchtype" operator="eq" value="810520006" />';
            entityfilter += '    <condition attribute="statecode" operator="eq" value="0" />';
            entityfilter += '</filter>';

            var linkfilter = '';
            if (document.getElementById("tSearchValue").value != "") {
                var fValue = document.getElementById("tSearchValue").value;
                linkfilter += '<filter type="and">';
                linkfilter += '    <filter type="or">';
                linkfilter += '        <condition attribute="firstname" operator="like" value="%' + fValue + '%" />';
                linkfilter += '        <condition attribute="lastname" operator="like" value="%' + fValue + '%" />';
                linkfilter += '        <condition attribute="fullname" operator="like" value="%' + fValue + '%" />';
                linkfilter += '        <condition attribute="userregno" operator="eq" value="' + fValue + '" />';
                linkfilter += '    </filter>';
                linkfilter += '</filter>';
            }

            var linkfilterevent = '<value uiname="' + v_event_name + '" uitype="dns_event">{' + v_event_value + '}</value>';

            fetchQuery = fetchQuery.replace('#ENTITY-FILTER#', entityfilter);
            fetchQuery = fetchQuery.replace('#LINK-ENTITY-FILTER#', linkfilter);
            fetchQuery = fetchQuery.replace('#LINK-ENTITY-event-FILTER#', linkfilterevent);
            //alert(fetchQuery);
            return fetchQuery;
        }

        function initIframe() {
            FetchIFrame.document.body.style.backgroundColor = "#E3EFFF";
            setIframeHeight('FetchIFrame');
            GeteventList();
            IFrameDisplayList();
        }
   </script>
</head>
<body onload="initIframe();" onresize="setIframeHeight('FetchIFrame');">
    <table style="width: 100%; height: 100%;" cellspacing="0" border="0">
        <tr>
            <td width="50%">
                <table width="100%"><tr>
                    <td class="Page-Header-Title">event<br />
                           <select class="Page-Select-Title" id="event" onchange="OnSearch();"></select>
                      </td>
                    <td class="Page-Header-Title">Match Status<br />
                           <select class="Page-Select-Title" id="matchtype" onchange="OnSearch();">
                               <option value="0">All</option>
                               <option value="82">No</option>
                               <option value="80" selected="selected">Maybe</option>
                               <option value="81">Yes</option>
                           </select>
                      </td>
                </tr></table>
            </td>
            <td width="50%" align="right">
                <table  width="100%"><tr>
                    <td class="Page-Header-Title">
                        Search for records <!--Mailing ID--><br />
                        <input style="width:50%" type="text" id="tSearchValue" operator="eq" onkeypress="checkEnter(event);" />
                        <input type="button" id="ButtonClear" onclick="Clear();" class="button" value="Clear" />
                        <input type="button" id="SearchButton" onclick="OnSearch();" class="button" value="Search" />
                    </td>
                </tr></table>
            </td>
        </tr>
    </table>
    <iframe id="FetchIFrame" name="FetchIFrame"  width="100%" height="100%" frameborder="0" scrolling="no">
    </iframe>
</body>
</html>

advancedfindview.js
var intRegex = /^\d+$/;

function getSize() {
    var myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myHeight = document.body.clientHeight;
    }
    return myHeight;
}

function checkEnter(e) { //e is event object passed from function invocation
    var characterCode; //literal character code will be stored in this variable

    if (e && e.which) { //if which property of event object is supported (NN4)
        e = e
        characterCode = e.which //character code is contained in NN4's which property
    }
    else {
        e = event
        characterCode = e.keyCode //character code is contained in IE's keyCode property
    }

    if (characterCode == 13) { //if generated character code is equal to ascii 13 (if enter key)
        OnSearch(); //submit the form
        return false
    }
    else {
        return true
    }
}

function Clear() {
    var fields = document.getElementsByTagName("input");
    for (var i = 0; i < fields.length; i++) {

        if (fields[i].type == "text") {
            if ((fields[i].value != null) && (fields[i].value != "")) {
                fields[i].value = "";
            }
        }
    }
}

function setIframeHeight(iframeName) {
    try {
        //alert(iframeName);
        var iframeEl = document.getElementById ? document.getElementById(iframeName) : document.all ? document.all[iframeName] : null;
        if (iframeEl) {
            iframeEl.style.height = "auto";
            iframeEl.style.height = getSize() - iframeEl.offsetTop + "px";
        }
    } catch (ex) { }
}

function DisplayAdvanceFindView(iframeId, entityName, advFindViewID, sortCol) {
    var Instance = this;
    var iframeDoc;

    Instance.RegisterOnTab = function (tabIndex) {
        Instance.Iframe = document.getElementById(iframeId);

        iframeDoc = getIframeDocument();
        var loadingGifHTML = "<table height='100%' width='100%' style='cursor:wait'>";
        loadingGifHTML += "<tr>";
        loadingGifHTML += "<td valign='middle' align='center'>";
        loadingGifHTML += "<img alt='' src='/_imgs/AdvFind/progress.gif'/>";
        loadingGifHTML += "<div/><b>Loading View...</b>";
        loadingGifHTML += "</td></tr></table>";
        iframeDoc.body.innerHTML = loadingGifHTML;

        Instance.Refresh();
    }

    Instance.Refresh = function () {
        try {
            iframeDoc = getIframeDocument();  //document.getElementById(iframeId).contentWindow.document; // target the docoment object of the page in the iframe
            var form = iframeDoc.createElement("form"); // create a form
            with (form) {
                setAttribute("name", "iframForm"); // give form a name
                setAttribute("action", "/DNS/AdvancedFind/fetchData.aspx"); // give form an action
                setAttribute("method", "post"); // give form a method
            }
            form.onsubmit = function () { //handle form submit with a message
                //alert("submit");
                form.submit();
                return false;
            }

            var input = iframeDoc.createElement("input"); // create an input element
            with (input) {
                setAttribute("name", "FetchXml"); // give input a name
                setAttribute("id", "FetchXml"); // give input a name
                setAttribute("type", "hidden"); // make it a text input
                setAttribute("value", getCRMFetchXML()); // give input a value
            }
            form.appendChild(input); // append input to form
            //alert(getCRMLayoutXML());

            var input = iframeDoc.createElement("input"); // create an input element
            with (input) {
                setAttribute("name", "LayoutXml"); // give input a name
                setAttribute("id", "LayoutXml"); // give input a name
                setAttribute("type", "hidden"); // make it a text input
                setAttribute("value", getCRMLayoutXML()); // give input a value
            }
            form.appendChild(input); // append input to form

            var input = iframeDoc.createElement("input"); // create an input element
            with (input) {
                setAttribute("name", "EntityName"); // give input a name
                setAttribute("id", "EntityName"); // give input a name
                setAttribute("type", "hidden"); // make it a text input
                setAttribute("value", entityName); // give input a value
            }
            form.appendChild(input); // append input to form

            var input = iframeDoc.createElement("input"); // create an input element
            with (input) {
                setAttribute("name", "DefaultAdvFindViewId"); // give input a name
                setAttribute("id", "DefaultAdvFindViewId"); // give input a name
                setAttribute("type", "hidden"); // make it a text input
                setAttribute("value", advFindViewID); // give input a value
            }
            form.appendChild(input); // append input to form

            var input = iframeDoc.createElement("input"); // create an input element
            with (input) {
                setAttribute("name", "ViewId"); // give input a name
                setAttribute("id", "ViewId"); // give input a name
                setAttribute("type", "hidden"); // make it a text input
                setAttribute("value", advFindViewID); // give input a value
            }
            form.appendChild(input); // append input to form

            var input = iframeDoc.createElement("input"); // create an input element
            with (input) {
                setAttribute("name", "ViewType"); // give input a name
                setAttribute("id", "ViewType"); // give input a name
                setAttribute("type", "hidden"); // make it a text input
                setAttribute("value", "1039"); // give input a value
            }
            form.appendChild(input); // append input to form

            var input = iframeDoc.createElement("input"); // create an input element
            with (input) {
                setAttribute("name", "SortCol"); // give input a name
                setAttribute("id", "SortCol"); // give input a name
                setAttribute("type", "hidden"); // make it a text input
                setAttribute("value", sortCol); // give input a value
            }
            form.appendChild(input); // append input to form
            iframeDoc.getElementsByTagName("body")[0].appendChild(form);
            iframeDoc.forms[0].onsubmit();
        }
        catch (err) {
            alert(err);
        }
    }

    function getIframeDocument() {
        return Instance.Iframe.contentWindow.document;
    }
}

function getCRMLayoutXML() {
    return fetchLayoutXML;
}
Event List - OData
function GetEventList() {
    var selectQuery = "gab_batchSet?$select=gab_batchId,gab_EventID,gab_name,gab_event_gab_batch_eventid/gab_eventDate,gab_event_gab_batch_eventid/gab_eventAltDate&$expand=gab_event_gab_batch_eventid&$orderby=CreatedOn desc&$filter=gab_BatchType/Value eq 82 and statecode/Value eq 0";
    var odataSelect = GetServerUrl() + selectQuery;
    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        async: false,
        datatype: "json",
        url: odataSelect,
        beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
        success: function (data, textStatus, XmlHttpRequest) {
            var result = data.d;
            if (!!result) {
                var evetns= result.results;
                var selected = 'selected';
                for (var i = 0; i < evetns.length; i++) {
                    //alert(JSON.stringifyevetns[i].gab_evetn_gab_batch_evetnid.gab_evetnDate));
                    var edate = convertJSONDate(evetns[i].gab_evetn_gab_batch_evetnid.gab_evetnDate);
                    if (i != 0) selected = '';
                    $("#election").append('<option value=' +evetns[i].gab_evetnID.Id + ' ' + selected + '>' + evetns[i].gab_evetnID.Name + " - " + edate + '</option>');
                }
                //IFrameDisplayList();
            }
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) {
            alert('OData Select Failed: ' + odataSelect);
        }
    });
}

No comments:

Post a Comment