Saturday, July 21, 2012

Conditionally Hiding OOB Ribbon Button in CRM 2011

Hi All, To hide the OOB Ribbon button permanently you can use my earlier post. But sometimes we needs to hide OOB Ribbon button conditionally in CRM 2011.  Here I am going to hide OOB "Assign" button conditionally based on Optionset Value.

       On the Account form, if the "Category"  optionset value is "Preferred Customer" then display the "Assign" button otherwise hide the "Assign" button.



1. Export the solution and open Customization.XML file in edit mode. Find the <RibbonDiffXml> at the Account form level.





2. Now open Accountribbon.xml file from the SDK and find the Assign button details at form Level

      
 Now copy the Assign button element from here and add this to custom Actions in the <RibbonDiffXML> which we are customizing.
 we also needs to copy the CommandDefinition of the OOB Assign button to provide OOB assign functionality. otherwise OOB Asign button functionality might not work.

3. Now edit the Account <RibbonDiffXML> in the following way

 <RibbonDiffXml>
        <CustomActions>
          <!-- Location will be ID of the Button-->
          <CustomAction Location="Mscrm.Form.account.Assign" Id="Sample.Form.account.HideAssign">
            <CommandUIDefinition>
              <!-- copy the OOB Asign button CommandUI Definition-->
              <Button Id="Mscrm.Form.account.Assign" ToolTipTitle="$Resources:Ribbon.HomepageGrid.MainTab.Actions.Assign" ToolTipDescription="$Resources(EntityPluralDisplayName):Ribbon.Tooltip.Assign" Command="Mscrm.AssignPrimaryRecord" Sequence="33" LabelText="$Resources:Ribbon.HomepageGrid.MainTab.Actions.Assign" Alt="$Resources:Ribbon.HomepageGrid.MainTab.Actions.Assign" Image16by16="/_imgs/ribbon/Assign_16.png" Image32by32="/_imgs/ribbon/Assign_32.png" TemplateAlias="o1" />
            </CommandUIDefinition>
          </CustomAction>
        </CustomActions>
        <Templates>
          <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
        </Templates>
        <CommandDefinitions>
          <!-- copy the OOB Asign button Command Definition-->
          <CommandDefinition Id="Mscrm.AssignPrimaryRecord">
            <EnableRules>
              <EnableRule Id="Mscrm.FormStateNotNew" />
              <EnableRule Id="Mscrm.AssignPrimaryPermission" />
              <EnableRule Id="Mscrm.NotOffline" />
            </EnableRules>
            <DisplayRules>
              <DisplayRule Id="Mscrm.AssignPrimaryPermission" />
              <DisplayRule Id="Mscrm.NotClosedActivity" />
              <!-- Add new display Rule for conditionally hiding-->
              <DisplayRule Id="Sample.Form.account.HideAssign.DisplayRule"/>
            </DisplayRules>
            <Actions>
              <JavaScriptFunction FunctionName="assignObject" Library="/_static/_forms/form.js">
                <CrmParameter Value="PrimaryEntityTypeCode" />
              </JavaScriptFunction>
            </Actions>
          </CommandDefinition>
        </CommandDefinitions>
        <RuleDefinitions>
          <TabDisplayRules />
          <DisplayRules>
            <!--Define the conditional Hiding Display Rule-->
            <DisplayRule Id="Sample.Form.account.HideAssign.DisplayRule">              
              <ValueRule Field="accountcategorycode" Value="1"/>
              <!--Field will be CRM field schema Name, Value will be optionset Value-->
            </DisplayRule>
          </DisplayRules>
          <EnableRules />
        </RuleDefinitions>
        <LocLabels />
      </RibbonDiffXml>
 

4. Now import the customization file.

Hope it helps!!!

Friday, July 13, 2012

Plugin on Retrieve Multiple Message in CRM 2011

Hi All, Now will see how to implement plugin on Retrieve Multiple Message. While Retrieving Data from an entity, I needs to check the query passed by the user and based on the filters, adding conditional filters and returning filtered information to the user.


        public void Execute(IServiceProvider serviceProvider)
        {
            // Extract the tracing service.
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            if (tracingService == null)
                throw new InvalidPluginExecutionException("Failed to retrieve the tracing service.");
            try
            {
                tracingService.Trace("Posting the execution context.");
                if (tracingService == null)
                    throw new InvalidPluginExecutionException("Failed to retrieve the service bus service.");
                
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                string city = string.Empty;

               if (context.MessageName == "RetrieveMultiple") 
                {
                    if (context.Depth <= 1)
                    {
                        IOrganizationServiceFactory factory =
                            (IOrganizationServiceFactory)
                            serviceProvider.GetService(typeof (IOrganizationServiceFactory));
                        IOrganizationService service = factory.CreateOrganizationService(null);


                        if (context.InputParameters.Contains("Query"))
                        {
                               // Get the query
                            QueryExpression query = (QueryExpression)context.InputParameters["Query"];
                          // get the conditions passed by the user
                            ConditionExpression[] filters = query.Criteria.Conditions.ToArray();

                            foreach (var filter in filters)
                            {
                                if (filter.AttributeName == "new_city")
                                    city = filter.Values[0].ToString();
                                //check if the query has city filter
                                if (!string.IsNullOrEmpty(city) && city == "MyCity")
                                {

                                        // write your logic,  adding additional filters conditionally or performing other tasks etc..
                                                                      
                                }
                            }
                           
                           
                        }
                       
                    }

                }
            }
            catch (FaultException<OrganizationServiceFault> e)
            {
                tracingService.Trace("Exception occured in the Plugin:" + e.ToString());
                // Handle the exception.
                throw;
            }
            catch (Exception ex)
            {
                tracingService.Trace("Exception: {0}", ex.ToString());
                throw;
            }
        }

Hope it helps!!!