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:

  1. Based on the Role of the current user.  (The system administrator defines which roles have permission to view each form)
  2. 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.
    FormSelector


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…

  1. 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  

  2. After a form is loaded, you can use javascript to navigate to another form:   Xrm.Page.ui.formSelector.items.get(itemId).navigate();

  3. The previous 2 options will work, but are not without their problems. 
    1. Navigating to a form using JavaScript presents a performance problem; basically the form has to be loaded twice whenever a user opens a record.
    2. 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). 
    3. 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);

       }