Using Client HTTP Stack with oData in Silverlight (CRM 2011)

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.


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

Share your thoughts

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 )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s