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
can this javascript get context or any parameter ?
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.
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);
}
}
}