How to connect to Dynamics CRM using Silverlight

This walkthrough shows you how you can use XrmLinq SL to connect to Dynamics CRM using Silverlight. XrmLinq allows you to use the same codebase (Entity Mapper generated code) against Dynamics CRM On-Premise and Partner Hosted versions. This gives you the ability to port your xRM solutions with simple configuration changes.
Note: To connect to Dynamics CRM using Silverlight, a special xml file(clientaccesspolicy.xml) has to be placed within the crm website. This is because of the silverlight security constraints.
Requirements
- XrmLinq SL 1.0 or above
- Access to Dynamics CRM and the server
- Visual Studio, .NET 3.5, Silverlight 3
Creating a Silverlight Solution
To begin, you need to create a new Silverlight 3 project. For this example we will be creating a simple silverlight application to create accounts in Dynamics CRM.
- Open Microsoft Visual Studio
- Create a new silverlight project (preferably C#)
Adding the required references
- Right click the project references(not the .Web project), click on Add Reference and add the reference to the following dlls
- Silverlight.Crm.Sdk
- XrmLinq.Silverlight
- System.Runtime.Serialization
- System.ServiceModel
- System.Xml.Linq
- System.Xml.Serialization
Creating the clientaccesspolicy.xml file
- Create a new file on the Dynamics CRM server called clientaccesspolicy.xml, it must be placed inside the \CRMWeb\ folder. Copy paste the following xml into that file then save and close.
<?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 include-subpaths="true" path="/"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
Let’s write some code
- Run the Entity Mapper to generate the code. (see the getting started guide if you haven’t already done so)
- Right click on the project and click on Add -> Existing Item -> Browse to the file path you specified at step 1.
- Build the project (Shift + Ctrl + B or Right click on the project -> Build) – the project should build without errors
- Add a new button to the MainPage.xaml file
<Button HorizontalAlignment=”Left” VerticalAlignment=”Top” Width=”75″ Content=”Click Me!” Name=”Button” Click=”Button_Click”/> - Copy paste the code below into the MainPage.xaml.cs file
private void Button_Click(object sender, RoutedEventArgs e)
{
XrmDataContext context = new XrmDataContext(
new Connection("http://server/mscrmservices/2007/crmservice.asmx",
"crm.org", "domain", "username", "password"));
// create an account
Account myAccount = new Account { Name = "XrmLinq" };
context.Create<Account>(myAccount);
}
Run the application to see the brand new account inside crm!
As you can see using XrmLinq allows you to re-use the same generated code file across Silverlight, ASP.NET, MVC, WinForms, WCF, WPF, Class libraries (Plugins, Workflows) and much much more, best of all you’re using the same syntax across all the projects. We make it very easy for you and your team to write applications by giving you strongly typed code and consistant syntax likq LINQ!




