RSS

Tag Archives: LookUp

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

Advertisements
 
2 Comments

Posted by on August 7, 2013 in WebResources

 

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

Filtering Lookup depending upon the OptionSet value

Introduction

We can filter the look up field depending upon the value in OptionSet field using javaScript.

Background

In our case, we have two entities “Account” and “Test”, which have 1:N relationship.
An OptionSet field “new_profession” and a lookup of the “Test” entity are present in “Account” entity.
To retrieve the filtered records depending upon the option selected in the “new_profession” field, we will implement a JScript on the load event of Account entity and OnChange event of the “new_profession” field in Account entity.

Code

function FilteredLookup() {

    var viewId = "{a76b2c46-c28e-4e5e-9ddf-951b71202c9d}"; //Any Guid is fine.
    var entityName = "new_test";// Entity to be filtered
    var profession = Xrm.Page.data.entity.attributes.get("new_profession").getValue();

    if(profession != null && profession != "")
    {
        //Name for the lookup field.
        Xrm.Page.ui.controls.get("new_test_testid").setDisabled(false);

        //Custom Name for the lookup window
        var viewDisplayName = Xrm.Page.data.entity.attributes.get("new_profession").getSelectedOption().text;

        var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                          "<entity name='new_test1'>" +
                              "<attribute name='new_name' />" +
                              "<attribute name='new_profession' />" +
                              "<order attribute='new_name' descending='false' />" +
                              "<filter type='and'>" +
                                "<condition attribute='new_profession' operator='eq' value='" + profession + "' />" +
                              "</filter>" +
                          "</entity>" +
                       "</fetch>";

        var layoutXml = "<grid name='resultset' " +
                            "object='1' " +
                            "jump='name' " +
                            "select='1' " +
                            "icon='1' " +
                            "preview='1'>" +
                            "<row name='result' " +
                                "id='new_test1id'>" + // Id attribute of the entity to be filtered
                                "<cell name='new_name' " + //Column 1 – Fullname
                                "width='100' />" +
                                "<cell name='new_profession' " + //Column 2- Profession
                                "width='100' />" +
                            "</row>" +
                        "</grid>";

        //Part3 add new view to the lookup
        Xrm.Page.getControl("new_test1_testid").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
    }
}

By Ranjan Parhi
Senior Software Engineer @Team DynamicsCRM.
Mindfire Solutions

 

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

 
%d bloggers like this: