Using CRM LookUp functionality in a Silverlight WebResource with CRM Look and Feel

07 Aug


In this blog, I will show you how to use the LookUp functionality of CRM in a Silverlight WebResource.

I am trying to create a LookUp for the Account inside a Contact record.


Let’s first go for the Design.

In the Design, let’s add a RichTextBox and a LookUp image similar to the CRM LookUp image.

<UserControl xmlns:sdk=""  x:Class="SilverlightApplicationLookUpDemo.MainPage"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <sdk:Label Height="20" Width="50" x:Name="lblLookup" HorizontalAlignment="Left" Content="Account" Margin="62,56,0,0" VerticalAlignment="Top"/>
        <RichTextBox Background="White" FontSize="10" VerticalScrollBarVisibility="Disabled" IsReadOnly="True" Height="21" HorizontalAlignment="Left" Margin="112,54,0,0" Name="txtLookUp" VerticalAlignment="Top" Width="200" TabIndex="1" />
        <Image Height="21" Width="21" HorizontalAlignment="Left" Margin="313,53,0,0" Name="ImgLookUp" MouseLeftButtonDown="LocalImage_MouseLeftButtonDown" Source="/SilverlightApplicationLookUpDemo;component/Images/btn_off_lookup.png" VerticalAlignment="Top"/>

It will look like below.


Code Behind

In the Code Behind, let’s insert the Event that will be fired, when the Image gets clicked.

The whole code will look something like this.

using System.Json;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

namespace SilverlightApplicationLookUpDemo
    public partial class MainPage : UserControl
        public MainPage()

        private void LocalImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            string lookupId = string.Empty;
            string lookupName = string.Empty;
            string contactId = "7C1EF3C4-39D3-E211-8A25-B4B52F566DF2";
            string baseUrl = "";
            string objectTypesContact = "2";

            //Relative URL for the LookUp
            string lookUpRecordUrl = "/_controls/lookup/lookupinfo.aspx?AllowFilterOff=1&DefaultType=1&" +
"DisableQuickFind=0&DisableViewPicker=0&LookupStyle=single&ShowNewButton=1&ShowPropButton=1&" +
"browse=false&currentid=" + contactId + "&objecttypes=" + objectTypesContact;

           //Complete URL to open the LookUp
           string lookUpUrl = baseUrl + lookUpRecordUrl;

           //Open the Modal Window for the LookUp
           dynamic returnedParams = HtmlPage.Window.Invoke("showModalDialog", lookUpUrl, null, "dialogWidth:500px;dialogHeight:700px");

           //For earlier Rollups, it will return a json object
           if (returnedParams.items)
               lookupId = returnedParams.items[0].id;
               lookupName = returnedParams.items[0].name;

           //For Rollup 12, as in Rollup 12 returnedparams would
           //be a json string instead of a json object.
               JsonValue jsonValue = JsonValue.Parse(returnedParams);
               JsonObject jsonObject = jsonValue as JsonObject;

               //user selected
               lookupId = jsonObject["items"][0]["id"].ToString();
               lookupName = jsonObject["items"][0]["name"].ToString();

           //Add style to the TextBox
           txtLookUp.Foreground = new SolidColorBrush(Colors.Blue);

           //Add selected item to the fiscal period TextBox
           Paragraph paragraph = new Paragraph();
           Underline underline = new Underline();
           Run run = new Run() { Text = lookupName };


Now let’s add the WebResource to a Contact entity.


So, after implementing, we will see something like this.




  1. For the demo, I have hard-coded the Contact Id and objecttype for Contact, but we can also pass them as QueryString Parameter/Custom Parameter for the Web Resource.
  2. For Server URL, we can use the “SilverlightUtility.GetServerBaseUrl()" function.
  3. The “SilverlightUtility.cs" can be found at location sdk\samplecode\cs\silverlight\soapforsilverlight\soapforsilverlightsample in the SDK provided by Microsoft.

By Dibyasingh Tripathy
Senior Software Engineer @Team DynamicsCRM.
Mindfire Solutions


Posted by on August 7, 2013 in WebResources


Tags: , , , , , , , , , , , , , , ,

2 responses to “Using CRM LookUp functionality in a Silverlight WebResource with CRM Look and Feel

  1. JasonF

    August 8, 2013 at 2:39 pm

    Hey take a look at: I have used this in the past and it works pretty good. Some qirks, but with the source it provides a good starting point.

  2. Sajan Choudhary

    October 15, 2013 at 8:22 am

    HtmlPage.Window.Invoke(“showModalDialog”, look this does not work for CRM 2013 in Google Chrome.
    Is there any alternative way to achieve it ?

    Thanks and Regards


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: