Featured Post

Web API Requests Series

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

20 October 2011

13 September 2011

Interact with CRM form objects from Silverlight through the HTML Bridge

In this post I will show you how we can access attributes on the form via Silverlight managed code.

When you host a Silverlight application in a HTML page you can access the HTML Document Object Model (DOM) from the managed code, and call managed code from JavaScript. This is possible using the HTML Bridge technology available in Silverlight.
In the same way you can access the xrm objects on a Dynamics Crm form using managed code and vice versa when a Silverlight application is embedded in a Crm form.
In this post I will show you a simple example of how to do this.

Recipe for this example:
  • I will create a simple Silverlight application
  • Create a Silverlight web resource
  • Add the web resource to a Crm form
  • Verify
The Silverlight application consists of a Textbox and a Button as shown below:

<StackPanel Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Stretch">
            <TextBox x:Name="InputText" Width="200" Height="23"/>
            <Button Content="Go" Width="22" Height="20" Click="Button_Click" Margin="4,0,0,0"/>
</StackPanel>

After having created the Silverlight application let's add the following code to the Button_Click  event handler:

add this a reference to the "System.Windows.Browser" to your Silverlight project and the using statement in order to be able to use the HTML Bridge,

using System.Windows.Browser;

private void Button_Click(object sender, RoutedEventArgs e)
        {
            //Get the xrm object using the dynamic type
            dynamic xrm = (ScriptObject)HtmlPage.Window.GetProperty("Xrm");
            //this is the number of employees attribute
            var numEmployees = xrm.Page.data.entity.attributes.get("numberofemployees");

            try
            {
               
                if (!string.IsNullOrEmpty(InputText.Text))
                {
                    //convert to int the input from SL text box
                    var employees = Convert.ToInt32(InputText.Text);

                    //set the attribute on the Crm form
                    numEmployees.setValue(employees);
                }
                else
                {
                    //set the attribute on the form
                    numEmployees.setValue(null);
                }
            }
            catch(System.FormatException fe)
            {
                HtmlPage.Window.Alert(string.Format("{0}", fe.Message));
            }
            catch(System.OverflowException ofe)
            {
                HtmlPage.Window.Alert(string.Format("{0}" , ofe.Message));
            }
        }

After having added the Silverlight web resource (see this post) we can now embed it in a form (the account form in this example), thje result is shown below:







To test the application enter a value into the textbox and enter "Go", you should see the number of employee populated with the value you have inserted in alternative you can leave the text box empty.
If you try to enter a value that is too large, too small or is not a number an exception will be thrown and a message will be displayed (see below) . Note that the messages are once again shown using the HTML Bridge through the following lines of code:

HtmlPage.Window.Alert(string.Format("{0}", fe.Message));
HtmlPage.Window.Alert(string.Format("{0}" , ofe.Message));


Alternatively we could have used a Silverlight Child window or simple MessageBox.Show("")

Well that's all really! Next time we will see how to call a Silverlight method from Crm form.

Stay tuned.

Luciano.




02 September 2011

12 August 2011

How to open a Silverlight application from the ribbon menu

In this post I am going to show you how to open a Silverlight application by clicking a custom ribbon button.

Steps:
  1. Create a new ribbon button
  2. Create the Silverlight application
  3. Create the XAP web resource
  4. Create a HTML web resource for the page that  hosts the Silverlight app
First of all let's create a new custom button, see this post.
Create your Silverlight application, if you want to use the REST endpoint see this post.
Create a Silverlight web resource and the HTML web resource.

Follow this guidelines when naming the web resources:

  1. HTML Web resource name = /MyApplicationName.html, this will result,after saving, in    customizationprefix_/MyApplicationName.html.
  2. HTML Web resource display name = MyApplicationName.html.
  3. XAP Web resource name = /ClientBin/MyApplicationName.xap, this will result,after saving, in                customizationprefix_/ClientBin/MyApplicationName.xap.
  4. XAP Web resource display name = MyApplicationName.xap.
Enjoy!


How to add a custom button in the ribbon menu.

In this post I will walk you through the process of creating a new ribbon button that will be able to open a HTML page from which we can access some crm context parameter such as language id and record id.
In this particular example I will be using the HTML page to host a Silverlight application.
First of all we need to export the solution that we want to customise, so navigate to settings-> Solutions and select the solution you want to export.
Once downloaded the solution open the customizations.xml file with your favourite editor.
Here is my customization:
<RibbonDiffXml>
        <CustomActions>
                <CustomAction Id="Imp.Form.Account.ShowHierarchy" Sequence="51" Location="Mscrm.Form.account.MainTab.ExportData.Controls._children">
                  <CommandUIDefinition>
                    <Button Id="Imp.Form.Account.ShowHierarchyButton" Image16by16="$webresource:new_Hierarchy16by16.gif" Image32by32="$webresource:new_Hierarchy32by32.gif" TemplateAlias="o1" LabelText="$LocLabels:Imp.Form.Account.ShowHierarchyTitle" ToolTipTitle="$LocLabels:Imp.Form.Account.ShowHierarchyTitle" ToolTipDescription="$LocLabels:Imp.Form.Account.ShowHierarchyTooltip" Command="Imp.Form.Account.ShowAccountHierarchyCommand" />
                  </CommandUIDefinition>
            </CustomAction>
                </CustomActions>
                <Templates>
                  <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
                </Templates>
                <CommandDefinitions>
                <CommandDefinition Id="Imp.Form.Account.ShowAccountHierarchyCommand">
                  <EnableRules></EnableRules>
                  <DisplayRules></DisplayRules>
                  <Actions>
                    <Url Address="$webresource:new_/SilverlightFromRibbonTestPage.html" PassParams="true"></Url>
                </Actions>
                </CommandDefinition>
                </CommandDefinitions>
                <RuleDefinitions>
                  <TabDisplayRules />
                  <DisplayRules />
                  <EnableRules />
                </RuleDefinitions>
                <LocLabels>
                <LocLabel Id="Imp.Form.Account.ShowHierarchyTooltip">
                    <Titles>
                      <Title languagecode="1033" description="This will open a HTML page hosting a Silverlight application." />
                    </Titles>
                </LocLabel>
                <LocLabel Id="Imp.Form.Account.ShowHierarchyTitle">
            <Titles>
              <Title languagecode="1033" description="Open Page" />
            </Titles>
        </LocLabel>
        </LocLabels>
      </RibbonDiffXml>

Import the new customization.xml file and if you don't have any error you should be able to see a new button on the account form.









Hope you find this post useful.
Cheers , Luciano.