Hi,
Just sharing a simple example that uses Client HTTP Stack while developing Silverlight web resource.
The good thing with this is that we don’t have to upload and publish the web resource for every change. However if the Siliverlight component is running in the context of the form and interacting with it then in that case we will have to upload and publish it.
Steps : –
Create a Silverlight application and add service reference to the oData service of CRM 2011.
Go to Settings |Customizations | Developer Resources
Get the url of the OrganizationData service

Add Service Reference to the URL

The context would be named as OrganizationNameContext.
Now in the MainPage.xaml set the url of the OrganizationData and Set up the context in the following manner.
public partial class MainPage : UserControl
{
private GGContext context;
private String orgDataServerUrl;
public MainPage()
{
InitializeComponent();
// set the server url
orgDataServerUrl = "http://server:port/GG/XRMServices/2011/OrganizationData.svc";
// set the context
context = new GGContext(new Uri(orgDataServerUrl));
// set up the Client Http Stack
context.HttpStack = System.Data.Services.Client.HttpStack.ClientHttp;
context.UseDefaultCredentials = false;
context.Credentials = new NetworkCredential("administrator", "password", "domain");
}
. . . .
Put the following clientaccesspolicy.xml in the following location
“..\Program Files\Microsoft Dynamics CRM\CRMWeb”
<?xml version="1.0" encoding="utf-8"?> <access-policy> <cross-domain-access> <policy> <allow-from http-request-headers="*"> <domain uri="*"/> </allow-from> <grant-to> <resource path="/" include-subpaths="true"/> </grant-to> </policy> </cross-domain-access> </access-policy>
Once we are done with development we can set up the context in the following manner
public partial class MainPage : UserControl
{
private GGContext context;
private String orgDataServerUrl;
public MainPage()
{
InitializeComponent();
// get the Server Url
orgDataServerUrl = this.GetServerUrlFromContext();
// setup Context
context = new GGContext(
new Uri(String.Format("{0}/xrmservices/2011/organizationdata.svc/",
orgDataServerUrl), UriKind.Absolute));
//This is important because if the entity has new
//attributes added the code will fail.
context.IgnoreMissingProperties = true;
}
private string GetServerUrlFromContext()
{
try
{
// 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);
}
return serverUrl;
}
catch
{
return String.Empty;
}
}
}
Hope this helps.
Discover more from Nishant Rana's Weblog
Subscribe to get the latest posts sent to your email.
