RSS

Tag Archives: Silverlight WebResource

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

Introduction

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.

Design

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="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="SilverlightApplicationLookUpDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    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"/>
    </Grid>
</UserControl>

It will look like below.

Capture

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()
        {
            InitializeComponent();
        }

        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 = "https://example.crm.dynamics.com";
            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.
           else
           {
               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 };

           underline.Inlines.Add(run);
           paragraph.Inlines.Add(underline);
           txtLookUp.Blocks.Clear();
           txtLookUp.Blocks.Add(paragraph);
       }
    }
}

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

Output

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

Image

Image

Notes

  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

 
2 Comments

Posted by on August 7, 2013 in WebResources

 

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

 
%d bloggers like this: