CRM 2011 + Accessing CRM Form elements using Javascript in Silverlight


In CRM 2011 when we create silverlight web resources we sometimes need to access the form data which can
be achieved in the following way.

HtmlPage.Window.Eval(“Place your javascript here”);

Sample Code:

HtmlPage.Window.Eval("Xrm.Page.data.entity.getId()");
Advertisements

3 thoughts on “CRM 2011 + Accessing CRM Form elements using Javascript in Silverlight

    1. Sayantan Samanta Post author

      I am not sure what exactly you(yukitaka) are talking about. But i think the following explanation might help.

      You can certainly add some parameters to the javascript. basically it is nothing but string that is executed at runtime.
      SO something like string s = “Xrm.Page.data.entity.getAttribute(‘ ” + attributeName + ” ‘)”; will work where attribute name is the parameter.

      Reply
  1. ota2000

    i found a sample that silverlight calls jscript in sdk document.
    serverUtility.cs class file is getting url by using Xrm.Page scripting library, not mentiond. http://msdn.microsoft.com/en-us/library/gg309558.aspx#Y3714
    it use invoke method that has parameter. then this was one answer.
    eval method witth string edit is another answer but it can’t pass object., i think.

    anyway your blog was my start point. thank you.

    i just made a silverlight button call form script that was originally onchange event driven one, and using form context, check some field calculating and write back.

    like follows…

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Data.Services.Client;
    using System.Windows.Browser;
    using SilverlightApplication1.ServiceReference2;

    namespace SilverlightApplication1
    {
    public partial class MainPage : UserControl
    {
    private atsContext _context = null;

    public MainPage()
    {
    InitializeComponent();

    if (_context == null)
    {
    // string _serverUrl = ServerUtility.GetServerUrl();
    // If the Silverlight is in a form, this will get the server url
    ScriptObject xrm = (ScriptObject)HtmlPage.Window.GetProperty(“Xrm”);
    ScriptObject page = (ScriptObject)xrm.GetProperty(“Page”);
    ScriptObject pageContext = (ScriptObject)page.GetProperty(“context”);

    String serverUrl = (String)pageContext.Invoke(“getServerUrl”);

    //The trailing forward slash character from CRM Online needs to be removed.
    if (serverUrl.EndsWith(“/”))
    {
    serverUrl = serverUrl.Substring(0, serverUrl.Length – 1);
    }

    App app = (App)Application.Current;
    app.ServerUrl = serverUrl;

    _context = new atsContext(
    new Uri(String.Format(“{0}/xrmservices/2011/organizationdata.svc/”,
    serverUrl), UriKind.Absolute));
    }

    // entity is always changing. should do this.
    _context.IgnoreMissingProperties = true;

    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
    // go the JScript with Form Context that’s all.
    HtmlPage.Window.Invoke(“NameofScript”, _context);
    }
    }
    }

    Reply

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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