locked
How to pass parameters in a HTML web resource (EntityName)? RRS feed

  • Question

  • Hi All,

    I have a requirement to show the attached image (Notes) in the form for which a HTML web resource is used. Below is the code used.

    I wanted to use the same JavaScript in other entities for the same requirement. So, I need to pass the entityname and relationship name (highlighted in the code) as parameters for my HTML Web Resource.

    I have seen some examples for passing multiple attributes of an entity but can anybody help me on how to pass EntityName and Relationship Name in my below code as parameters by which I can use it in different entities.

     

    $(document).ready(function ()
    {
        $("#container").hide();

        var x = XrmServiceToolkit;
        var id = window.parent.Xrm.Page.data.entity.getId();
        var entity = "new_applicationSet(guid'" + id + "')/new_application_Annotations";
        var filter = "?$filter=endswith(MimeType,'jpeg') or endswith(MimeType,'jpg') or endswith(MimeType,'gif') or endswith(MimeType,'png')&$top=1&$orderby=CreatedOn desc";

        x.Rest.RetrieveMultiple(
            entity,
            filter,
            function (results)
            {
                if (results.length == 1)
                {
                    var src;

                    var name = results[0].FileName.toLowerCase();
                    if (name.match(/jpeg$/))
                        src = "data:image/jpeg;base64,";
                    else if (name.match(/jpg$/))
                        src = "data:image/jpeg;base64,";
                    else if (name.match(/png$/))
                        src = "data:image/png;base64,";
                    else if (name.match(/gif$/))
                        src = "data:image/gif;base64,";

                    src += results[0].DocumentBody;

                    $("#container").attr('src', src);
                    $("#container").show();
                    $("#msg").hide();
                }
            },
            function (error) {
                $("#msg").text(error.message);
            },
            function () { },
            true
        );
    });

    Thanks & Regards,

    Sri

     
    Thursday, June 11, 2015 8:14 AM

All replies

  • If I remember correct there is a checkbox for each web resource where you can have it send CRM parameters.

    Here is a list of parameters passed and their names, https://msdn.microsoft.com/en-us/library/gg309536.aspx

    Also you should be able to access the parent entity from your javascript by using window.parent.Xrm.Page.data.entity.getEntityName()

    Something like.

    var entity = window.parent.Xrm.Page.data.entity.getEntityName() + "Set(guid'" + id + "')/new_application_Annotations";


    Halldór Jóhannsson



    Thursday, June 11, 2015 9:51 AM
  • Hi

    You can get entity name using bellow code

    var entName=window.parent.Xrm.Page.data.entity.getEntityName()

    var entity = "new_"+entName+"Set(guid'" + id + "')/new_"+entName+"_Annotations";

    try with this 

    Wednesday, June 24, 2015 6:00 AM
  • see the code

    <!DOCTYPE html >
    <html lang="en-us">
    <head>
     <title>Show Data Parameters Page</title>
     <style type="text/css">
      body
      {
       font-family: Segoe UI, Tahoma, Arial;
       background-color: #d6e8ff;
      }
      tbody
      {
       background-color: white;
      }
      th
      {
       background-color: black;
       color: White;
      }
     </style>
     <script type="text/javascript">
      document.onreadystatechange = function () {
       if (document.readyState == "complete") {
        getDataParam();
       }
      }
    
      function getDataParam() {
       //Get the any query string parameters and load them
       //into the vals array
    
       var vals = new Array();
       if (location.search != "") {
        vals = location.search.substr(1).split("&");
        for (var i in vals) {
         vals[i] = vals[i].replace(/\+/g, " ").split("=");
        }
        //look for the parameter named 'data'
        var found = false;
        for (var i in vals) {
         if (vals[i][0].toLowerCase() == "data") {
          parseDataValue(vals[i][1]);
          found = true;
          break;
         }
        }
        if (!found)
        { noParams(); }
       }
       else {
        noParams();
       }
      }
    
      function parseDataValue(datavalue) {
       if (datavalue != "") {
        var vals = new Array();
    
        var message = document.createElement("p");
        setText(message, "These are the data parameters values that were passed to this page:");
        document.body.appendChild(message);
    
        vals = decodeURIComponent(datavalue).split("&");
        for (var i in vals) {
         vals[i] = vals[i].replace(/\+/g, " ").split("=");
        }
    
        //Create a table and header using the DOM
        var oTable = document.createElement("table");
        var oTHead = document.createElement("thead");
        var oTHeadTR = document.createElement("tr");
        var oTHeadTRTH1 = document.createElement("th");
        setText(oTHeadTRTH1, "Parameter");
        var oTHeadTRTH2 = document.createElement("th");
        setText(oTHeadTRTH2, "Value");
        oTHeadTR.appendChild(oTHeadTRTH1);
        oTHeadTR.appendChild(oTHeadTRTH2);
        oTHead.appendChild(oTHeadTR);
        oTable.appendChild(oTHead);
        var oTBody = document.createElement("tbody");
        //Loop through vals and create rows for the table
        for (var i in vals) {
         var oTRow = document.createElement("tr");
         var oTRowTD1 = document.createElement("td");
         setText(oTRowTD1, vals[i][0]);
         var oTRowTD2 = document.createElement("td");
         setText(oTRowTD2, vals[i][1]);
    
         oTRow.appendChild(oTRowTD1);
         oTRow.appendChild(oTRowTD2);
         oTBody.appendChild(oTRow);
        }
    
        oTable.appendChild(oTBody);
        document.body.appendChild(oTable);
       }
       else {
        noParams();
       }
      }
    
      function noParams() {
       var message = document.createElement("p");
       setText(message, "No data parameter was passed to this page");
    
    
       document.body.appendChild(message);
      }
      //Added for cross browser support.
      function setText(element, text) {
       if (typeof element.innerText != "undefined") {
        element.innerText = text;
       }
       else {
        element.textContent = text;
       }
    
      }
     </script>
    </head>
    <body>
    </body>
    </html>
    

    Wednesday, June 24, 2015 6:48 AM