CRM Plugin reset form

Having recently been playing with multiple forms in CRM 2011, I wanted to make sure that when opening a record, the user would always be presented with the main information form. At first, I decided to udpate the UserUIEntitySettings from within the form onload. But I must say I had a few problems with this option:

  1. First, I decided to use a getFormId javascript function. It worked fine until I realized most users were using CRM with a language that was different from the base language. This made the form id’s unaccessible to them, and thus the function always returned nothing.
  2. Second, I decided to use the Xrm.Page.ui.formSelector object. But for some reason, while this worked well in the main form, it turned out that in the other form, the Xrm.Page.ui.formSelector.items collection was always null. I still did not figure out why.

So in the end I decided to go the plugin way, and implemented the below function in a PreRetrieve event. Notice that the mainFormId parameter is passed to the plugin using the secure/unsecure parameters (unsecure in this case), since it is not accessible for retrieve to most users because of their current language.

I also have to retrieve the concerned entity’s object type code. Indeed, you must be careful if you use early binding in your plugin NOT TO use new_entity.EntityTypeCode, as this will be different between environments.

private void ResetMainForm(context)

        {
            RetrieveEntityRequest entityRequest = new RetrieveEntityRequest
            {
                EntityFilters = EntityFilters.All,
                LogicalName = account.EntityLogicalName
            };
            RetrieveEntityResponse entityResponse = (RetrieveEntityResponse)context.OrganizationService.Execute(entityRequest);
            QueryExpression query = new QueryExpression(UserEntityUISettings.EntityLogicalName);
            query.Criteria.AddCondition(“ownerid”, ConditionOperator.Equal, context.PluginExecutionContext.UserId.ToString());
            query.Criteria.AddCondition(“objecttypecode”, ConditionOperator.Equal, entityResponse.EntityMetadata.ObjectTypeCode.Value.ToString());
            query.ColumnSet = new ColumnSet(true);
            EntityCollection UISettingsCollection = context.OrganizationService.RetrieveMultiple(query);
            if (UISettingsCollection.Entities.Count > 0)
            {
                //update the last viewed formId:
                UserEntityUISettings settings = (UserEntityUISettings)UISettingsCollection[0];
                settings.LastViewedFormXml = “<MRUForm><Form Type=”Main” Id=”” + mainFormId + “” /></MRUForm>”;
                context.OrganizationService.Update(settings);
            }
        }

[ad]

Leave a Reply

Your email address will not be published. Required fields are marked *