Sub Title

Thursday, April 5, 2012

CRM5 Passing Multiple Values to a Web Resource Through the Data Parameter

A Web page (HTML) or Silverlight Web resource page can only accept a single custom parameter called data. To pass more than one value within the data parameter, you need to encode the parameters and decode the parameters within your page.

The page below represents a technique to pass the additional values within a single parameter and then process them within your Web resource.



Sample HTML Web Resource


The HTML code below represents a Web page (HTML) Web resource that includes a script that defines function:

function qs(search_for) {
var query = window.location.search.substring(1);
query = unescape(query);
var parms = query.split('&');
for (var i = 0; i < parms.length; i++) { var pos = parms[i].indexOf('='); if (pos > 0 && search_for == parms[i].substring(0, pos)) {
return parms[i].substring(pos + 1);
}
}
return "";
}

Using this page

For dynamic values generated in code, use the encodeURIComponent method on the parameters. The encoded values should be:

first%3DFirst%20Value%26second%3DSecond%20Value%26third%3DThird%20Value

Open the page passing the encoded parameters as the value of the data parameter:

http:///WebResources/new_/ShowDataParams.htm?Data=first%3DFirst%20Value%26second%3DSecond%20Value%26third%3DThird%20Value

Note

If you have added the Web resource to a form and have pasted the un-encoded parameters into the Custom Parameters(data) field, you can just preview the form.

The new_/ShowDataParams.htm will display a dynamically generated table:


first:: First Value
second:: Second Value
third:: Third Value



How to pass parameters to Silverlight controls from ASP.NET pages ?

You can pass parameters from your aspx pages and html pages to the Silverlight controls. This chapter explains how to pass parameters to Silverlight controls from your aspx page and code behind files.

InitParameters

The Xaml page user control has a property called InitParameters. You can set a value in the form of key-value pairs from your ASPX pages. Since this property accepts key-value pairs, you can pass any set of string values.

How to set InitParameters

Example - Set InitParameters property from ASPX page:


<asp:Silverlight 
   ID="Xaml1" 
   runat="server" 
   Source="~/ClientBin/MySilverlightApp.xap" 
   InitParameters="City=Houston,State=Texas,Country=USA"
   Width="300" 
   Height="300" />


You can set this property from the code behind file of your ASPX page as well.

Example - Set InitParameters property from the code behind file:
Xaml1.InitParameters = "City=Houston,State=Texas,Country=USA";


How to retrieve the InitParameters value ?

The value you pass to a Silverlight control through the InitParameters property can be retrieved from the Application_Startup even handler in the App.xaml page.
private void Application_Startup(object sender, StartupEventArgs e)
{
    IDictionary parameters = e.InitParams;

    this.RootVisual = new Page1();


Now, in most cases, you may want to pass this value to the xaml page itself instead of doing anything with in it the App.xaml.

Pass parameters from App.xaml to other pages

To pass the parameters from the App.xaml pages to other pages, you must modify the constructor of xaml page class to accept parameters.
private IDictionary<string, string> parameters = null;

public Page1()
{
    InitializeComponent();
}

public Page1(IDictionary<string, string> p)
{
    this.parameters = p;

    InitializeComponent();
}


The above sample shows an additional constructor added which takes a parameter of type IDictionary and then sets the value to a member variable.

Now go back to your App.xaml and pass the parameters to the page:
private void Application_Startup(object sender, StartupEventArgs e)
{
    IDictionary parameters = e.InitParams;

    this.RootVisual = new Page1(parameters);


Use the IDictionary parameter values in the XAML pages

If you have followed the above steps correctly, you should be able to access the IDictionary values in your XAML page.
textblock1.Text = this.parameters["City"];


Here is the complete code from the XAML page:
<UserControl x:Class="MySilverlightApp.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300" Loaded="UserControl_Loaded">

    <Grid x:Name="LayoutRoot" Background="White">

<TextBlock x:Name="textblock1" Width="200" Height="30"></TextBlock>

    </Grid>
</UserControl>


In the code behind file for your page1.xaml, you can set the Text for the textblock1 control as shown below:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
        textblock1.Text = parameters["City"];
}