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!!!

4 comments:

  1. hi Guru this is an old post but just wanted to know what do you register this plugin against? Is it the entity or a view ?

    ReplyDelete
    Replies
    1. Hi, I have registered this plugin against an entity. when someone queries this entity either from code/gridview, plugin gets triggered.

      Delete
  2. hi Guru,
    I want to restrict Offline Mode in Dynamics CRM 2011 for Outlook client , for the machines that are not in my domain.
    I mean i want to do device authentication when a user logons from home.
    How can i achieve this requirement ? Is it possible through plugin ?

    ReplyDelete
    Replies
    1. Why do you need offline mode if you wants to restrict user's accessing CRM outside the domain.

      I think you can write some offline plugin to check the user system details.

      Delete