Microsoft Dynamics CRM 4.0 :Developing a Basic Silverlight Application

This section walks you through the basic steps necessary to create a project inside Microsoft Visual Studio 2008, using Microsoft Dynamics CRM data and then displaying within Microsoft Dynamics CRM. In this project, we use the Visual Studio add-in for Silverlight. In this foundation project, we build a UI component using a VS.NET project and a data access component using WCF:


1. Open Visual Studio 2008.

2. Select File, New, and then Project.

3. In the Project selection window, select Silverlight Application (as shown in Figure 1).

Figure 1. Project selection in Visual Studio 2008.

4. Click OK.

5. In the Add Silverlight Application dialog box, select Automatically Generate a Test Page to Host Silverlight at Build Time. For production release, you can attach this to an existing ASP.NET application. By default, this will create two files in the Solution Explorer:

  • App.xaml is a file used to declare shared resources such as data grids and various style objects. The code behind this file is used for handling global events such as Application_Startup, Application_Exit, and Application_UnhandledException (see Figure 2).

    Figure 2. Show sample app.xaml.
  • Page.xaml is the default file created. Typically, this is replaced with the new Silverlight page (see Figure 3).

    Figure 3. Show sample page.xaml.

Create a WCF project in the same solution. WCF is used as the communication layer for the Silverlight application. WCF supports asynchronous connections between the Silverlight application and the backend data sources. Figure 4 shows the communication sequence between the client and the backend when a user is viewing CRM notes on the Account page using Silverlight and all the steps in between.

Figure 4. Sample of the process to extract data for Silverlight.

1. Right-click the solution and create a new WCF project.

2. Click Add.

3. Select New Project.

4. Select WCF Service Application, as shown in Figure 5.

Figure 5. Select WCF Service Application.

Once the new project is attached to the solution, build the necessary connection to the CRM system, whether it is using web services or direct SQL access. Some organizations prefer direct SQL access for reading, whereas other organizations prefer to use the web services.It is recommended to use Microsoft Dynamics CRM web services to read and write data. However, if your application is only reading data, you can access the SQL views and access data very easily.

Proxy Credentials for web requests

TO make WebRequests from behind a firewall, we need to attach a proxy object to our request.

    
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
//Create proxy credentials
ICredentials credential = new NetworkCredential(uid, password, domain);
//Create proxy server. byPassListArray is a string array of local ip's to bypass
IWebProxy proxyServer = new WebProxy(proxy,true,byPassListArray,credential);

request.Proxy = proxyServer;
request.Credentials = new NetworkCredential(Uid,Password);

example code snippet:

string query="http://www.feedster.com/search.php?type=rss&q=" +srch+ "&sort=date&limit=100";
WebProxy proxyObj = new WebProxy("10.10.249.19", 8080) ;
NetworkCredential networkCredential = new NetworkCredential("yourname", "yourpass") ;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(query) ;
proxyObj.Credentials = networkCredential ;
req.Proxy = proxyObj ;
// this request uses the default credential set--
req.Credentials = System.Net.CredentialCache.DefaultCredentials ;
// or we can create unique new credentials:
//request.Credentials = new NetworkCredential(Uid,Password);

XmlDocument doc = new XmlDocument();
System.Net.HttpWebResponse resp =(HttpWebResponse)req.GetResponse() ;
doc.Load(resp.GetResponseStream());
XmlDocumentFragment frag = doc.CreateDocumentFragment();
XmlNode nod=doc.SelectSingleNode("//channel/item");