One of the new features in CRM 2011 is the ability to finally define multiple forms for a single entity. CRM allows you to determine which form a user sees in two ways:
- Based on the Role of the current user. (The system administrator defines which roles have permission to view each form)
- The user can switch between multiple forms using a dropdown menu. CRM will automatically remember which form the user last selected and show them the same form the next time they open a record.
However, what if you want to determine which form a user sees based on some other criteria? There are a few things you can do…
- Navigate to the URL for a specific form. Forms can be navigated to directly by using a URL in the following format (the part in red is the ID of the form you want to display):
http://mycrm/myOrg/main.aspx?etn=account&pagetype=entityrecord&extraqs=formid%3D6009c1fe-ae99-4a41-a59f-a6f1cf8b9daf%0D%0A
- After a form is loaded, you can use javascript to navigate to another form: Xrm.Page.ui.formSelector.items.get(itemId).navigate();
- The previous 2 options will work, but are not without their problems.
- Navigating to a form using JavaScript presents a performance problem; basically the form has to be loaded twice whenever a user opens a record.
- And specifying the formId in the URL only works if you’re opening the form from a custom UI (that is, you can’t specify a formid when the user opens a record from a normal CRM view).
- Also, once a user opens a specific form CRM will remember that as the last selected form and the user will get the same form the next time they open a record, even if the formid is not provided in the URL.
Fortunately, there’s an easy way around these issues. It doesn’t seem to be documented in the SDK yet, but CRM saves the Id of the last form that a user viewed using the UserEntityUISettings entity, and any UserEntityUISettings record can be easily updated to set the Last Viewed form for a specific user:
//retrieve the user UI settings for a specific user and a specified entity:
QueryExpression query = new QueryExpression(UserEntityUISettings.EntityLogicalName);
query.Criteria.AddCondition("ownerid", ConditionOperator.Equal, userId);
query.Criteria.AddCondition("objecttypecode", ConditionOperator.Equal, entityObjectTypeCode);
EntityCollection UISettingsCollection = service.RetrieveMultiple(query);
if (UISettingsCollection.Entities.Count > 0)
{
//update the last viewed formId:
UserEntityUISettings settings = (UserEntityUISettings)UISettingsCollection[0];
settings.LastViewedFormXml = "<MRUForm><Form Type=\"Main\" Id=\"f5cfab6a-d4c2-4519-b68f-6e7485432e29\" /></MRUForm>";
service.Update(settings);
}
The next time the user opens any record for the specified entity, they will view the form that is specified in the LastViewedFormXml. No need to specify a formId in the URL, and no need to redirect the user to a different form in JavaScript.
~Erik Pool
This posting is provided "AS IS" with no warranties, and confers no rights.
//retrieve the user UI settings for a specific user and a specified entity:
QueryExpression query = new QueryExpression(UserEntityUISettings.EntityLogicalName);
query.Criteria.AddCondition("ownerid", ConditionOperator.Equal, userId);
query.Criteria.AddCondition("objecttypecode", ConditionOperator.Equal, entityObjectTypeCode);
EntityCollection UISettingsCollection = service.RetrieveMultiple(query);
if (UISettingsCollection.Entities.Count > 0)
{
//update the last viewed formId:
UserEntityUISettings settings = (UserEntityUISettings)UISettingsCollection[0];
settings.LastViewedFormXml = "<MRUForm><Form Type=\"Main\" Id=\"f5cfab6a-d4c2-4519-b68f-6e7485432e29\" /></MRUForm>";
service.Update(settings);
}
I tried the Pre-Retrieve plugin to set the LastViewedFormXml but it did not work cause CRM gets the LasViewedFormXml before it gets the entity record.
Posted by: Gilmar | 10/03/2011 at 11:11 AM
Hi Charlie, for that you could use the formselector navigate() javascript function to re-navigate to the correct form during the onload() event. Another possibility (which I haven't tried yet) would be to have a pre-retrieve plug-in on your entity that would set the LastViewedFormXml for the current user based on some value of the current record. I'm not sure if this would really work though; it would depend on when CRM determines which form to use (if it's after the plug-in fires I think it would work).
Posted by: Erik Pool | 09/19/2011 at 10:21 AM
Thanks Erik! Quick question: Is there a way to force the form to open in the version that it was saved in? We have a few users that work across multiple sales departments (and multiple opportunity forms). When they open an opportunity, we need it to open in the appropriate form version regardless of the form version they last viewed. Thank you!
Posted by: Charlie | 09/18/2011 at 02:38 PM