Thursday, 20 August 2009

Getting the dialog arguments passed into a web page modal dialog

This is especially useful if you use Microsoft CRM. Because it ties in very closely to how the entity forms and grid views communicate to custom web pages etc.

For example, in Microsoft CRM you can edit the ISVConfig to add a button to the toolbar for the entity grid. Set the WinMode=2 (2 basically means a modal dialog) to allow CRM to provide you with the selected entities. So how do we know what items were selected in the entity list? It passes us in a list of IDs as part of the window.dialogArguments.

So, how do we get access to this? Through javascript. Here's the steps:

1. Create the following javascript function (available from MSCRM help pages):

function loadSelectedListItems() {
  var sGUIDValues = "";
  var selectedValues;
  //Make sure window.dialogArguments is available.
  if (window.dialogArguments) {
    selectedValues = new Array(window.dialogArguments.length - 1);
  else {
  selectedValues = window.dialogArguments;
  if (selectedValues != null) {
    for (i = 0; i < selectedValues.length; i++) {
      sGUIDValues += selectedValues[i] + ",";
    document.getElementById('<%= winDialogArguments.ClientID %>').value = sGUIDValues;

2. Use this function as part of the onload of the body:
<body onload="loadSelectedListItems()">

3. Create a new holder object (in my example an asp textbox) in a hidden division on your form:
<div style="visibility: hidden">
   <asp:textbox id="winDialogArguments" runat="server" text="">

Job done. Just access the Text of the asp textbox server side on the next post back (e.g. click of an OK button) to get the list of IDs.

No comments:

Post a Comment