Featured Post

Web API Requests Series

Web API Series:  Create and Retrieve , Update and Delete , Retrieve Multiple , Associate/Disassociate , Impersonation , Run Workflows

08 April 2011

Silverlight app outside CRM context

Recently I have been working on a project where I needed to create a Silverlight application that ran outside the CRM context and I needed to pass parameters to the applicaiton from CRM context.
In a previous post I have talked of how to pass parameter to a Silverlight web resource within CRM context.
So, likely in this project the Silverlight application was not totally disconnected from CRM(of course) but it was launched by clicking a custom ribbon button from CRM. This, in fact, made things a little bit easier.
When you customize the ribbon menu in CRM 2011 by adding a new button for example, you may want to pass input parameter to a Java Script function for instance, well this is possible by adding the appropriate tags to the customization.
Let's start from exporting the customization from our CRM organization, extract the archive, open the customization.xml file with Visual Studio or another XML editing application that supports XSD schema validation. "This will help avoid XML validation errors when you import the ribbon. Associate the customizations.xml file with the customizationsSolution.xsd file. You can find this schema in the SDK\Schemas\CustomizationsSolutions.xsd file in the SDK download package."(from the CRM 2011 SDK).
Alternatively you can export only the Ribbon by creating a new unmanaged solution for this purpose, this will reduce the amont of data that you export/import and time.
Once you have downloaded the solution open the customizations.xml with your favourite editor, locate the RibbonDiffXml node, it is here that you need to apply your customization, you can find more details on how to customize a Ribbon in the CRM 2011 SDK. However here is one of mine:


<RibbonDiffXml>
  <CustomActions>
    <CustomAction Id="AP.Pharmacy.Form.CustomAction" Location="Mscrm.Form.hd_pharmacy.MainTab.Actions.Controls._children" Sequence="21">
      <CommandUIDefinition>
        <Button Id="AP.Pharmacy.Form.Button.AddPrescription" Command="AP.Pharmacy.Form.CommandDefinition.AddPrescription" LabelText="Add Prescription" ToolTipTitle="Add Prescription" ToolTipDescription="Lookup for patient and add Prescription" TemplateAlias="o1" Image16by16="/_imgs/ribbon/AddExistingStandard_16.png" Image32by32="/_imgs/ribbon/AddExistingStandard_32.png" />
      </CommandUIDefinition>
    </CustomAction>
    <CustomAction Id="AP.Pharmacy.HomepageGrid.CustomAction" Location="Mscrm.HomepageGrid.hd_pharmacy.MainTab.Actions.Controls._children" Sequence="21">
      <CommandUIDefinition>
        <Button Id="AP.Pharmacy.HomepageGrid.Button.AddPrescription" Command="AP.Pharmacy.HomepageGrid.CommandDefinition.AddPrescription" LabelText="Add Prescription" ToolTipTitle="Add Prescription" ToolTipDescription="Lookup for patient and add Prescription" TemplateAlias="o1" Image16by16="/_imgs/ribbon/AddExistingStandard_16.png" Image32by32="/_imgs/ribbon/AddExistingStandard_32.png" />
      </CommandUIDefinition>
    </CustomAction>
  </CustomActions>
  <Templates>
    <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
  </Templates>
  <CommandDefinitions>
    <CommandDefinition Id="AP.Pharmacy.HomepageGrid.CommandDefinition.AddPrescription">
      <EnableRules>
        <EnableRule Id="AP.contact.WebClient.EnableRule" />
        <EnableRule Id="AP.contact.grid.OneSelected.EnableRule" />
      </EnableRules>
      <DisplayRules>
        <DisplayRule Id="AP.contact.WebClient.DisplayRule" />
      </DisplayRules>
      <Actions>
        <JavaScriptFunction Library="$webresource:AA_/AddNewSample.js" FunctionName="AddNewRecordFromHomepageGrid">
          <CrmParameter Value="SelectedControlSelectedItemIds" />
          <CrmParameter Value="UserLcid" />
        </JavaScriptFunction>
      </Actions>
    </CommandDefinition>
    <CommandDefinition Id="AP.Pharmacy.Form.CommandDefinition.AddPrescription">
      <EnableRules>
        <EnableRule Id="AP.contact.WebClient.EnableRule" />
      </EnableRules>
      <DisplayRules>
        <DisplayRule Id="AP.contact.WebClient.DisplayRule" />
      </DisplayRules>
      <Actions>
        <JavaScriptFunction Library="$webresource:AA_/AddNewSample.js"   FunctionName="AddNewRecordFromForm">
          <CrmParameter Value="FirstPrimaryItemId" />
          <CrmParameter Value="UserLcid" />
        </JavaScriptFunction>
      </Actions>
    </CommandDefinition>
  </CommandDefinitions>
  <RuleDefinitions>
    <TabDisplayRules />
    <DisplayRules>
      <DisplayRule Id="AP.contact.WebClient.DisplayRule">
        <CrmClientTypeRule Type="Web" />
      </DisplayRule>
      <DisplayRule Id="AP.contact.form.FormStateNotNew.DisplayRule">
        <FormStateRule State="Create" InvertResult="true" />
      </DisplayRule>
    </DisplayRules>
    <EnableRules>
      <EnableRule Id="AP.contact.WebClient.EnableRule">
        <CrmClientTypeRule Type="Web" />
      </EnableRule>
      <EnableRule Id="AP.contact.form.NotNew.EnableRule">
        <FormStateRule State="Create" InvertResult="true" />
      </EnableRule>
      <EnableRule Id="AP.contact.grid.OneSelected.EnableRule">
        <SelectionCountRule AppliesTo="SelectedEntity" Maximum="100" Minimum="1" />
      </EnableRule>
    </EnableRules>
  </RuleDefinitions>
  <LocLabels />
</RibbonDiffXml>

As you can see in the highlighted part you can call a javascript function that must be a web resource and pass parameter to this function, here there are only three parameter shown, you can find more on the SDK. So once you click the button this function gets called and then from there you can call the HTML page that hosts the Silverlight application and pass those parameters to Silverlight.
Hope this helps.

No comments:

Post a Comment