In Dynamics CRM 2011, one of the new features is sub-grid. It gives the users with proper security the ability to view, create, and modify records for a related entity directly within a form. As shown in the image below, in the form of Firm entity, there is a sub-grid of Documents, and when that sub-grid is selected the application ribbon dynamically changes to reflect the options available for that entity.
If we click the “Add Existing Document” button, the following lookup window for Document will pop up.
The URL of this page will be something like: http://yourdomain.com/_controls/lookup/lookupinfo.aspx?LookupStyle=multi&browse=0&objecttypes=10089
So what happen if we need to append some extra parameters to this URL and those parameters will be used by our plugin to add extra criteria to the filter for the record? Like if we want to append “firmId” to the URL. After much debugging of the system JavaScripts, I found the solution.
First of all, you need to create a new ribbon button in the SubGrid area of Document entity. This button will call our custom function that we will create in our next step. If you got any questions regarding this step, you check out this tutorial provided by Microsoft.
<CustomAction Id="CustomAddExistingButton"
Location="Mscrm.SubGrid.document.MainTab.Management.Controls._children" Sequence="10">
<CommandUIDefinition>
<Button Id="Mscrm.SubGrid.document.CustomAddExistingButton"
Command="CustomAddExistingButtonAction"
Sequence="30" LabelText="Add Existing" Alt="Add Existing"
Image16by16="/_imgs/ribbon/AddExistingStandard_16.png"
Image32by32="/_imgs/ribbon/AddExistingStandard_32.png"
TemplateAlias="o1" ToolTipTitle="Add Existing"
ToolTipDescription="Add Existing" />
</CommandUIDefinition>
</CustomAction>
<CommandDefinition Id="CustomAddExistingButtonAction">
<EnableRules />
<DisplayRules />
<Actions>
<JavaScriptFunction
FunctionName="CustomAddExistingRecord"
Library="$Webresource:ribbon.js">
<CrmParameter Value="PrimaryEntityTypeCode" />
<CrmParameter Value="PrimaryItemIds" />
<CrmParameter Value="SelectedEntityTypeCode" />
<CrmParameter Value="SelectedControl" />
</JavaScriptFunction>
</Actions>
</CommandDefinition>
Then create this function in your JavaScript web resource file:
CustomAddExistingRecord = function (primaryTypeCode, progressId, typeCode, control) {
var firmId = parent.Xrm.Page.getAttribute("nzte_primaryfirmid").getValue()[0].id;
var extraParams = "&firmId=" + firmId;
var relName = control.getParameter("relName");
var roleOrd = control.getParameter("roleOrd");
var lookupItems = window.parent.LookupObjects(null, "multi", typeCode, 0, null, extraParams, null, null, null, null, null, null, null);
if (lookupItems) {
if (lookupItems.items.length > 0) {
if (AssociateObjects(primaryTypeCode, progressId[0], typeCode, lookupItems, roleOrd == 2, null, relName)) {
}
}
}
}
Basically what this JavaScript function does it, get the value of firmId and add it into the extraParams variable. Then call “window.parent.LookupObjects” function with the extra parameters, at this moment, the lookup window will pop up, and we can limit the records returned using a plugin, I will talk about this in my next blog.
Once the user selects a product, then we call the “AssociateObjects” function, with the values for all the “CrmParameters” passed from our ribbon button. This function is for associating the record user selected with the main record. That’s all you need to do to modify the CRM default “Add Existing” functionality. The final result it below: