August 30, 2019 12:31 by
Peter
The Problem
Recently I was creating a Silverlight based app for one of our customers on my Silverlight 5 Hosting. The applications runs on the contact form embedded in an IFrame. It used OData to retrieve a defined set of columns for the contact. All was looking nice, until I noticed, that after changing some of the columns and saving the contact, my Silverlight application still used old data – even after refreshing the page.
Internally it used an HttpWebRequest object to retrieve data from CRM via a URL like /XrmServices/2011/OrganizationData.svc/ContactSet(GUID’{0}’)?$select=FullName. Unfortunately Silverlight aggressively caches Web-Requests. Since the URL for a contact never changes, Silverlight does not even really do a further request and pulls data from the cache instead. The HttpWebRequest class in the Silverlight Framework does not provide means to disable caching. There are a lot of posts and forum topics out there discussing that problem.
The solution
Obviously the problem is, that the URL does not change. To overcome the caching issue, we just have to make sure, the address changes on every request. The solution I came up with was simply adding a made up parameter to the URL. Fortunately the OData Endpoint simply ignores unknown parameters. I called it ignoreCache, but of course any name will do here. The value of that parameter will be set to basically the current time. Now the URL changes on every request and we can work with the actual data:
Dim urlRaw As String = String.Format(“/XrmServices/2011/OrganizationData.svc/ContactSet(GUID’{0}’)?$select=FullName&IgnoreCache={1}”, id, Date.Now.TimeOfDay.Ticks)
August 16, 2019 09:39 by
Peter
Today, I am going to show you how to fixed Silverlight 5 Control Content always null from Javascript Access. I got an Error when I try to access the Silverlight object from the client side. Here’s the following snippet code from client site.
function search() {
try {
var silverLightControl = document.getElementById("silverlightControl");
silverLightControl.Content.Page.SetUser(document.getElementById("txtUser").value);
} catch (e) {
alert(e.description);
}
}
The page with silverlight object embedded
<div id="silverlightControl">
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="ClientBin/SLAspxCommunication.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="5.0.61118.0" />
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=5.0.61118.0" style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
</a>
</object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
At the silverlight object class, we have registered the page as javascriptable object with following line
HtmlPage.RegisterScriptableObject("Page", this);
public MainPage()
{
InitializeComponent();
_users = GenerateList();
HtmlPage.RegisterScriptableObject("Page", this);
}
The function looks simple that I just want to call the Silverlight function to search the user, unfortunately. This error message below always popped up.
When I debug the process, it indicate that the control did not contain a Content element. This Error
var silverLightControl = document.getElementById("silverlightControl");
It will load the Div Control instead of the object container that host the silverlight object. After I set the id to be silverlightControl for the object tag. then search function funtion well and access the SetUser function in the silverlight object.
<div>
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%" id="silverlightControl">
<param name="source" value="ClientBin/SLAspxCommunication.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="5.0.61118.0" />
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=5.0.61118.0" style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
</a>
</object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>