Advertisements
Home > CRM 2011, Plug-Ins > Setting default view using Plug-in’s in CRM 2011

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.

'Contacts' link

  • “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 🙂 @@

Advertisements
  1. Pradeep
    April 16, 2012 at 11:19 AM

    Thanks man, I was looking for something like this . Before I get start with this i just want to confirm on the second part of your code , i meant following code,
    ///

    /// Setting IsDefault property of views

    ///

    private EntityCollection ChangeViewCollection(string ourDefaultViewName, EntityCollection records){

    foreach (Entity record in records.Entities){

    …………………………….
    …………………………….

    what is this code and how to use it ??!!

    Thanks

  2. April 16, 2012 at 1:37 PM

    Hi,
    The method ChangeViewCollection() is use to set the “isdefault” property of the view’s.

    The arguments of this method are
    i) String ourDefaultViewName -> View name which u want it to be as Default
    ii) EntityCollection records -> Collection of views of the perticular entity

    – Basically we loop through all the views and if the view name matches with the “View Name” we passed as argument, we set the “isdefault” property of the view to true and break the loop.

  3. Steph
    September 10, 2012 at 2:51 PM

    Hi Rajeev,
    Many thanks for your post.
    Actually it works like a charm when on the workplace. For example I can change the default view when the link ‘Activities’ is clicked.
    However when I am on an account form and then click on the ‘Activities’ menu, the default view can’t be changed using the plugin. The code is executed but the “isdefault” value gets overriden somewhere.
    Do you have an idea how it works ? I have been looking for a solution to this functionality for a long time and couldn’t find any yet.

    Thank you,
    Steph

  4. Siva M
    February 19, 2013 at 4:32 PM

    Hi Rajeev,

    Thanks for the Plugin code, but its not working for me, and i am not getting any error also.
    registration and steps are also correct and u declared a varialbe ‘viewsToHide ‘ and u didnt used it any where. Please help me resolve the issue.
    Thanks in advance.

    • February 28, 2013 at 11:17 AM

      Hi Siva,
      Try to debug and see the View name you passes is matching with existing views.
      Let me know if you able to resolve

  5. Madhavi
    May 6, 2013 at 11:19 PM

    Hi Rajeev,

    Thanks for the sample code. I could execute it without any errors and noticed that the isdefault property was set to true for the view which was selected as default view. But I didn’t see any change in the output.

    Please let me know what else I should add to make it work.

    Thank you.

  6. Rupesh
    June 12, 2013 at 8:38 PM

    Hi Rajeev,

    Thanks for the code. it is really helpful.
    I have written the code to update the fetchxml of the view.
    But if i update from the collection entity i am not able to update the fecthxml. and there is no change in my view.
    I mean this is approach not working for updating fetch xml of the view for me.
    please can you provide me any solution on this issue?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: