HTML Bridge in Silverlight


Hi,

Just sharing simple example.

Suppose we have the following HTML page having a Silverlight control (blue background) in it.

When we click on “Pass to Silverlight” button we want to pass the values specified in the textboxes in our HTML page to the text boxes in the Silverlight control.

To do this we can create a function inside our Silverlight page (MainPage.xaml.cs) and decorate it with ScriptableMember attribute and also use RegisterScriptableObject method.

 [System.Windows.Browser.ScriptableMember]
 public void SetLabelFromHtml(string firstName, string lastName)
 {
 txtFirstName.Text = firstName;
 txtLastName.Text = lastName;
 }
 
public MainPage()</pre>
 {
 InitializeComponent();
 // RegisterScriptableObject Method
 // Registers a managed object for scriptable access by JavaScript code
 System.Windows.Browser.HtmlPage.RegisterScriptableObject("myMethod", this);
 }
<pre> 

To call this function from our html page, we can make use of following JavaScript.

function btnPass_onclick() {

// id of the silverlight control in object tag
var silverlightPlugin = document.getElementById("mySilverLightControl");
// get the values from the text boxes in the html page
var firstName = document.getElementById("txtFirstName").value;
var lastName = document.getElementById("txtLastName").value;

silverlightPlugin.content.myMethod.SetLabelFromHtml(firstName, lastName);

}

Similarly if we want to pass the values for the text boxes from Silverlight page to our HTML page, we need to do the following.

First create a JavaScript function in out HTML page

function SetValueFromSilverlight(firstName, lastName) {

document.getElementById("txtFirstName").value = firstName;
 document.getElementById("txtLastName").value = lastName;

}

To call this JavaScript function from our Silverlight page we can make use of following line of code

HtmlPage.Window.CreateInstance("SetValueFromSilverlight",new string[] { txtFirstName.Text,txtLastName.Text});

Apart from calling the JavaScript function inside our html, we can directly access the fields through DOM and set their values in Silverlight

HtmlDocument document = HtmlPage.Document;

// get the text boxes
HtmlElement firstName = document.GetElementById("txtFirstName");
HtmlElement lastName = document.GetElementById("txtLastName");

// get the value in html text boxes
MessageBox.Show("First Name is " + firstName.GetProperty("value").ToString());
MessageBox.Show("Last Name is " + lastName.GetProperty("value").ToString());

// set the html text boxes
firstName.SetProperty("value", txtFirstName.Text);
lastName.SetProperty("value", txtLastName.Text);

Bye.

An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail.


We were getting the above error while testing one of our WCF service. On syncing the clock of our client and server machine we were able to resolve the issue.

This helped us resolve the issue

http://social.msdn.microsoft.com/Forums/eu/wcf/thread/eccf2b09-6ea1-4663-9ddf-af29901ea60a

Bye.

 

Sample Code to Cancel All the Open Activities for a particular record in CRM 2011.


Hi,

Just sharing a sample code that could be used to cancel all the open activities for a particular record.

 private void CancelChildActivities(Guid entityRecordGuid, IOrganizationService service)
 {
 QueryExpression qe = new QueryExpression();
 qe.ColumnSet = new ColumnSet(new string[] { "activityid", "activitytypecode" });
 qe.EntityName = "activitypointer";

FilterExpression filterStateCode = new FilterExpression();
 filterStateCode.FilterOperator = LogicalOperator.Or;
 filterStateCode.AddCondition("statecode", ConditionOperator.Equal, "Open");
 filterStateCode.AddCondition("statecode", ConditionOperator.Equal, "Scheduled");

FilterExpression filter = new FilterExpression();
 filter.FilterOperator = LogicalOperator.And;
 filter.AddCondition("regardingobjectid", ConditionOperator.Equal, entityRecordGuid);
 filter.AddFilter(filterStateCode);

qe.Criteria = filter;

RetrieveMultipleRequest request = new RetrieveMultipleRequest();
 request.Query = qe;
 RetrieveMultipleResponse response = (RetrieveMultipleResponse)service.Execute(request);

foreach (Entity activity in response.EntityCollection.Entities)
 {
 CancelActivity(activity, service);
 }

}

 private void CancelActivity(Entity entity, IOrganizationService service)
 {
 EntityReference moniker = new EntityReference();

if (entity.LogicalName == "activitypointer")
 {
 if (entity.Attributes.Contains("activityid") & entity.Attributes.Contains("activitytypecode"))
 {
 moniker.LogicalName = entity.Attributes["activitytypecode"].ToString();
 moniker.Id = (Guid)entity.Attributes["activityid"];

SetStateRequest request = new SetStateRequest();
 request.EntityMoniker = moniker;
 request.State = new OptionSetValue(2);
 request.Status = new OptionSetValue(-1);
 SetStateResponse response = (SetStateResponse)service.Execute(request);
 }
 }

}

Bye.

Customization (Unsupported) of Lookup in CRM 2011.


Hi,

For disabling view button, disabling “look for” option or hiding Button in lookup we can make use of JavaScript.

We need to refer to our lookup through DOM in our onload event and set its attributes (unsupported).

Suppose our lookup id is customerid

document.getElementById(“customerid”).setAttribute(“disableViewPicker“, “1”);


document.getElementById(“customerid”).setAttribute(“disableQuickFind“, “1”); 


document.getElementById(“customerid”).setAttribute(“_lookupbrowse“, “1”); 

Hiding New Button

function myHideLookupButton()

{

crmForm.all.customerid.attachEvent(“setadditionalparams“,HideButton);

}

function HideButton()

{

crmForm.all.customerid.AddParam(“ShowNewButton“, 0);

}

Bye.

Filtered lookup for n:n relationship.


Hi,

Daniel Cai has recently posted about his solution for filtered lookup in case of n:n relationship in his blog.

It works like a charm.

Do check it out.

http://danielcai.blogspot.com/2011/12/filtered-lookup-for-existing-button-of.html

Bye.

Calling Organization.svc from JavaScript in CRM 2011.


Hi,

There is a SOAPLogger tool that ships with CRM 2011 SDK which can be used to capture HTTP request and response.

http://msdn.microsoft.com/en-us/library/gg594434.aspx

Using that tool we can write our code in C# and it captures the corresponding SOAP Request and Response for it in a file named Output.txt, which we can then use in our JavaScript.

Sample SetStateRequest code

Request


Response

I have simply modified the tool to output the JavaScript as well which we can then use in onload or onchange event in CRM.

JavaScript

Download

Hope it helps.

Nishant Rana's Weblog

Everything related to Microsoft .NET Technology

Skip to content ↓