Thursday, April 3, 2014

Open a html webresource as a window or a dialog box in CRM 2011.

Hi Guys,

If you need to open a web resource as a window or as a dialog in Dynamics CRM 2011 you can use these methods. But you need to know somethings:
- When you open a dialog you cannot use anymore F12 key to debug javascript code in your called webresource.
- An opened dialog cannot refresh or close a parent window. If you need to refresh the parent entity form you need to use openWebResourceAsWindow() insteed of openWebResourceAsDialog(). because, in the window mode you can use :

window.opener.location.reload();


Source Code
  1.  
  2. /// <summary>
  3. /// Allow to open a html webresource as a dialog box
  4. /// </summary>
  5. /// <param name="webResourceName"><b>string</b>: HTML web resource logical name like (new_webresource.html)</param>
  6. /// <param name="webResourceData"><b>string</b>: URI Encoded web resource data using encodeURIComponent() that begins with "?"</param>
  7. /// <param name="width"><b>int</b>: the width of the window without (px)</param>
  8. /// <param name="height"><b>int</b>: the height of the window without (px)</param>
  9. /// <param name="viewScrollbars"><b>bool</b>: allow to view the scrollbars (true or false)</param>
  10. /// <param name="viewToolbar"><b>bool</b>: allow to view the toolbar (true or false)</param>
  11. /// <param name="viewMenubar"><b>bool</b>: allow to view the menubar (true or false)</param>
  12. /// <param name="viewLocation"><b>bool</b>: allow to view the location (true or false)</param>
  13. function openWebResourceAsDialog(webResourceName, webResourceData, width, height, viewScrollbars, viewToolbar, viewMenubar, viewLocation) {
  14.     var $v_0 = Mscrm.CrmUri.create(String.format("$webresource:{0}", webResourceName));
  15.  
  16.     if (!isNullOrEmptyString(webResourceData)) {
  17.         $v_0 = $v_0 + webResourceData;
  18.     }
  19.     var parameters = "";
  20.     if (viewScrollbars !== undefined && viewScrollbars == true) {
  21.         parameters = "scrollbars=1,";
  22.     }
  23.     else {
  24.         parameters = "scrollbars=0,";
  25.     }
  26.     if (viewToolbar !== undefined && viewToolbar == true) {
  27.         parameters += "toolbar=1,";
  28.     }
  29.     else {
  30.         parameters += "toolbar=0,";
  31.     }
  32.     if (viewMenubar !== undefined && viewMenubar == true) {
  33.         parameters += "menubar=1,";
  34.     }
  35.     else {
  36.         parameters += "menubar=0,";
  37.     }
  38.     if (viewLocation !== undefined && viewLocation == true) {
  39.         parameters += "location=1";
  40.     }
  41.     else {
  42.         parameters += "location=0";
  43.     }
  44.  
  45.     return openStdDlg($v_0, null, width, height, true, false, parameters);
  46. }
  47.  
  48. /// <summary>
  49. /// Allow to open a html webresource as a window
  50. /// </summary>
  51. /// <param name="webResourceName"><b>string</b>: HTML web resource logical name like (new_webresource.html)</param>
  52. /// <param name="webResourceData"><b>string</b>: URI Encoded web resource data using encodeURIComponent() that begins with "?"</param>
  53. /// <param name="width"><b>int</b>: the width of the window without (px)</param>
  54. /// <param name="height"><b>int</b>: the height of the window without (px)</param>
  55. /// <param name="viewScrollbars"><b>bool</b>: allow to view the scrollbars (true or false)</param>
  56. /// <param name="viewToolbar"><b>bool</b>: allow to view the toolbar (true or false)</param>
  57. /// <param name="viewMenubar"><b>bool</b>: allow to view the menubar (true or false)</param>
  58. /// <param name="viewLocation"><b>bool</b>: allow to view the location (true or false)</param>
  59. function openWebResourceAsWindow(webResourceName, webResourceData, width, height, viewScrollbars, viewToolbar, viewMenubar, viewLocation) {
  60.     var $v_0 = Mscrm.CrmUri.create(String.format("$webresource:{0}", webResourceName));
  61.  
  62.     if (!isNullOrEmptyString(webResourceData)) {
  63.         $v_0 = $v_0 + webResourceData;
  64.     }
  65.     var parameters = "";
  66.     if (viewScrollbars !== undefined && viewScrollbars == true) {
  67.         parameters = "scrollbars=1,";
  68.     }
  69.     else {
  70.         parameters = "scrollbars=0,";
  71.     }
  72.     if (viewToolbar !== undefined && viewToolbar == true) {
  73.         parameters += "toolbar=1,";
  74.     }
  75.     else {
  76.         parameters += "toolbar=0,";
  77.     }
  78.     if (viewMenubar !== undefined && viewMenubar == true) {
  79.         parameters += "menubar=1,";
  80.     }
  81.     else {
  82.         parameters += "menubar=0,";
  83.     }
  84.     if (viewLocation !== undefined && viewLocation == true) {
  85.         parameters += "location=1";
  86.     }
  87.     else {
  88.         parameters += "location=0";
  89.     }
  90.     return openStdWin($v_0, null, width, height, parameters)
  91. }
  92.  
  93. /// <summary>
  94. /// Allows to open the custom Discount Dialog
  95. /// </summary>
  96. function openDiscountDialog() {
  97.     if (Xrm.Page.data.entity.getIsDirty()) {
  98.         alert("One or more fields are modified. Please save the form.");
  99.         return;
  100.     }
  101.     var entityId = Xrm.Page.data.entity.getId();
  102.     var entityName = Xrm.Page.data.entity.getEntityName();
  103.     var customParameters = encodeURIComponent("?EntityName=" + entityName + "&EntityId=" + entityId);
  104.     openWebResourceAsDialog("njl_discountDialog.html", customParameters, 280, 130, false, false, false, false);
  105. }
  106.  
  107. /// <summary>
  108. /// Allows to open the custom Incident Resolution Window
  109. /// </summary>
  110. function openIncidentResolutionWindow() {
  111.     if (Xrm.Page.data.entity.getIsDirty()) {
  112.         alert("One or more fields are modified. Please save the form.");
  113.         return;
  114.     }
  115.     var entityId = Xrm.Page.data.entity.getId();
  116.     var entityName = Xrm.Page.data.entity.getEntityName();
  117.     var customParameters = encodeURIComponent("?EntityName=" + entityName + "&EntityId=" + entityId);
  118.     openWebResourceAsWindow("njl_incidentResolutionWindow.html", customParameters, 400, 280, true, false, false, true);
  119. }

Enjoy yourself,
N.JL

2 comments:

  1. Never say there is nothing beautiful in the world anymore. There is always something to make you wonder in the shape of a tree, the trembling of a leaf. See the link below for more info.


    #anymore
    www.ufgop.org

    ReplyDelete
  2. Excellent! Help me so much!! Congrats!!

    ReplyDelete