title: Dynamics CRM 2011 - Changing the view for a related entity list
body: <p>I have a couple of custom entities, which are linked together with a many to many relationship, using a linking entity which contains some linking attributes. For the sake of clarity let's call these Superhero and Supervillain, and the link will be Fight.</p>
  
  <pre><code>(Superhero) 1 --------- * (Fight) * -------- 1 (Supervillain)
  </code></pre>
  
  <p>That is, each fight is between 1 superhero and 1 supervillain, and each superhero/suppervillain combo can have any number of fights maybe of different type - sword, fist, etc.</p>
  
  <p>All simple stuff so far. Effectively, what I'm doing here is reproducting the new "Connections" functionality, but using a custom linking type that only works with Superheros and Supervillains. The reason for this is I want to show these as a separate navigation item in the left hand side of the form, rather than lump them all in under "Connections".</p>
  
  <p>Now, from the Superhero form, when I look at the related entities in the left hand pane, I want to show a see a list of the Fights, along with the Supervillain that the Superhero was fighting. Simple enough, I can just customise the "Fight Associated View" to show the type of Fight, and the Supervillain entity. This lets me click straight through from the Superhero to the Supervillain he was fighting.</p>
  
  <p>But how about if I want to do the same from Supervillain? The same "Fight Associated View" is shown for Supervillains as well, but needs to be customised to show the fight type and the related Superhero... But, I don't want a "one size fits all" associated view, with both Superhero and Supervillain fields.</p>
  
  <p>I want the associated view to change according to whether I'm looking at a Superhero or a Supervillain. So I create two new views, each with the desired columns. I can now <em>manually</em> switch to these view when selecting the related list in each entity. This works fine, but I don't want this manual step every time I look at the Related fights.</p>
  
  <p>So, finally I get to the point of the <strong>question</strong>: How can I automatically (i.e. through script, if necessary) select the default "Associated" view that will be shown for the Fights related to Superheros and Supervillains, dependent on the entity type?</p>
  
  <p>Using form script, I can get the navigation item easily enough, but after that I am lost:</p>
  
  <pre><code>function SuperHero_OnLoad()
  {
      var fightItem = Xrm.Page.ui.navigation.items.get("nav_new_fight");
      // What next?!
      fightItem.MagicFunctionThatSelectsADefaultViewOrHacksIntoTheIframeParametersOrSomething();
  }
  </code></pre>
  
  <p><strong>Note:</strong> I know I can use a sub-grid in the main edit form, which lets you choose the view to display, but I don't want to clog the form up with more sub-grids. I want to use the Related tree on the left of the form.</p>