Create a Plugin to Retrieve multiple message in CRM 2011 (crm tutorial)

Written by admin. Posted in CRM TUTORIAL, PLUGIN

Tagged: , , , , , , ,

Published on October 05, 2013 with No Comments

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

Just to Share My Knowledge on Microsoft Dynamics CRM…

No Comments

Comments for Create a Plugin to Retrieve multiple message in CRM 2011 (crm tutorial) are now closed.