Archive
Creating a plug-in on SavedQuery entity using CRM developer toolkit
In one of my requirement I have to create a plugin on “SavedQuery” entity to filter the views
I was using CRM developer toolkit and when I open the “CRM Explorer” I did not find “SavedQuery” entity
Later I came to know that “CRM Explorer” show entities by ‘Display Name’ and there was entity with name ‘View’ for ‘SavedQuery’
Thought of sharing this though its simple observation
🙂
Setting default view using Plug-in’s in CRM 2011
Hi,
We often get the requirement to set a default view based on some criteria (Ex – Could be based on logged in user role, etc…). We can achieve this using a Plug-in.
Little insight :-
- When you click on an entity (i.e., ‘Contacts’ in my sample) in CRM application, In the background a “RetreieveMultiple” method call happens on “savedquery” entity.
- “savedquery” is a system entity in CRM which stores the meta data of all the views in the system
Plug-in Logic :-
- We register a Plug-in for the RetreieveMultiple message on the savedquery entity to be executed in the post stage
- From “Outputparameters” fetch the “BusinessEntityCollection” (i.e., Views of particular entity)
- Loop through the views and set “isdefault” property to “true” for the desired view
– Below is the sample code
public void Execute(IServiceProvider serviceProvider)
{
this.context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = serviceFactory.CreateOrganizationService(this.context.UserId);
if (this.context.InputParameters.Contains(“Query”) && this.context.InputParameters[“Query”] is QueryExpression && this.context.Stage == 40){
var qe = (QueryExpression)this.context.InputParameters[“Query”];
if (qe.Criteria != null) {
var condition = (ConditionExpression)qe.Criteria.Conditions[1];
// “Type code” of entity you want default views ( Object type code of ‘contact’ is 2)
// Refer Useful article on how to get “Object Type Code”
if (condition.Values[0].Equals(2)) {
// Name of the view you want to set as Default
string ourDefaultView = “My custom view – Contacts”;
if (!string.IsNullOrEmpty(ourDefaultView)){
var collection = (EntityCollection)this.context.OutputParameters[“BusinessEntityCollection”];
collection = ChangeViewCollection(ourDefaultView, collection);
this.context.OutputParameters[“BusinessEntityCollection”] = collection;
}
}
}}}
/// <summary>
/// Setting IsDefault property of views
/// </summary>
private EntityCollection ChangeViewCollection(string ourDefaultViewName, EntityCollection records){
foreach (Entity record in records.Entities){
string viewName = (string)record[“name”];
if (viewName.Equals(ourDefaultViewName)){
record[“isdefault”] = new bool();
record[“isdefault”] = true;
}
else {
record[“isdefault”] = new bool();
record[“isdefault”] = false;
}
}
return records;
}
Plug-in Registration Steps :-
Message Name – RetrieveMultiple
Entity – SavedQuery
Stage – post
- Register the assembly & Refresh the CRM application
@@ 🙂 Here goes my 50th article. Thanks for all your visits and valuable comments 🙂 @@