The Microsoft.Crm.Sdk.Mobile source code sample is a partial re-implementation of the Microsoft Dynamics CRM SDK classes written as a Portable Class Library to facilitate development of store apps for Windows 8.1 desktop, tablets, and phones. The code makes use of the SOAP and OData protocols to issue web service method calls. An organization web service proxy and most of the message response/request classes in the CRM SDK have been implemented. When writing apps that use this code, you don’t have to link to the CRM SDK assemblies to access the organization web service.
This code can also be used when you develop apps using Xamarin. However, you must comment out the EnableProxyTypes method in Microsoft.Xrm.Sdk.Samples.cs when developing iOS or Android apps using Xamarin because that method contains code that is specific to Windows Store. An alternative is to comment out the relevant code as shown here.
public async Task EnableProxyTypes()
List<TypeInfo> typeList = new List<TypeInfo>();
//// Obtain folder of executing application.
//var folder = Package.Current.InstalledLocation;
//foreach (var file in await folder.GetFilesAsync())
// // Not only .dll but .exe also contains types.
// if (file.FileType == ".dll" || file.FileType == ".exe")
// var assemblyName = new AssemblyName(file.DisplayName);
// var assembly = Assembly.Load(assemblyName);
// foreach (TypeInfo type in assembly.DefinedTypes)
// // Store only CRM entities.
// if (type.BaseType == typeof(Entity))
types = typeList.ToArray();
Building the Sample
Your development computer must have an internet connection to build the library as a NuGet package named Json.NET will be automatically downloaded during the build.
To build the Microsoft.Crm.Sdk.Mobile.dll assembly, follow these steps.
- In Visual Studio 2013, with update 2 or later installed, load the Microsoft.Crm.Sdk.Mobile.sln file.
- Press F6.
The Microsoft Dynamics CRM 2013 SDK (and earlier releases) support client and server side code development for Dynamics CRM. However, the SDK assemblies cannot be used to develop mobile and store applications as the Windows run-time (WinRT) is not supported. To provide support for mobile apps, including cross platform iOS and Android apps, the Dynamics CRM server now supports client access to the organization web service using industry standard protocols such as OAuth (for authentication), plus SOAP and OData for sending web service requests. The problem is that while these protocols are supported, they are difficult to use. The solution is to layer a framework on top of the protocols to make app programming easier. For developing mobile and store apps, you can use the Microsoft Azure Active Directory Library (ADAL) for authentication, and this sample helper code library for web service requests and early-bound type support.
What is included in the library
This library contains the following key components:
Web Service Proxy
This library provides most methods that the OrganizationServiceProxy class in the CRM SDK does - Create, Update, Assign, Execute, and so on. The class supports early-bound development by providing an EnableProxyTypes method. SOAP implementations of these methods use the same name as are used in the CRM SDK while the OData-based methods have names that begin with “Rest” for example RestCreate.
SDK classes and enumerations
This library supports most common CRM SDK classes and enumerations. With the addition of early-bound type support, you can use Visual Studio IntelliSense when writing your apps.
Organization web service messages
This library includes request/response classes for over 200 messages. It covers both data operations, for example Create, and metadata operations, for example RetrieveMetadataChanges.
What isn't included in the library
Several functional areas that the library doesn’t cover are as follows:
Since this sample code library targets multiple platforms, and each platform has its unique way to authenticate the user, the library doesn’t provide any authentication mechanism. You must obtain an OAuth access token and pass it to an OrganizationDataWebServiceProxy object to access the organization web service. It’s recommended that you use the Microsoft Azure Active Directory Authentication Library (ADAL) for identity authentication. This library is referenced in the CRM SDK documentation and sample code. There is an open source implementation of ADAL available for iOS and Android. There is a Windows 8.1 version available for the desktop, tablets, and phones.
For more information on ADAL see http://www.cloudidentity.com/blog/2014/06/16/adal-for-windows-phone-8-1-deep-dive.
A sample code file named CRMHelpers.cs is provided to demonstrate how to authenticate a universal app. The file is not included in the build of the library.
DiscoveryService, DeploymentService, OrganizationServiceContext
This library doesn’t provide support for the discovery or deployment web services. It also doesn’t support the OrganizationServiceContext class.
How to write apps that use this library
You can build Microsoft.Crm.Sdk.Mobile.dll using the supplied Visual Studio 2013 solution and add a reference for it in your app’s project or you can add the library’s C# sample code files to your project.
In your app, add code to authenticate the user and obtain a security access token. The method used varies depending on platform as mentioned previously. Next, instantiate the OrganizationDataWebServiceProxy class.
OrganizationDataWebServiceProxy _proxy = new OrganizationDataWebServiceProxy();
Set the access token and web server URL in the OrganizationDataWebServiceProxy object.
_proxy.ServiceUrl = "<your CRM server URL goes here>";
_proxy.AccessToken = "<access token>";
Call EnableProxyTypes to use early-bound types and optionally assign a service time-out interval.
_proxy.Timeout = 600;
Send a message request to the web service by using an async/await pattern.
WhoAmIResponse whoAmIResponse = (WhoAmIResponse)await _proxy.Execute(new WhoAmIRequest());
// Retrieve User Info
SystemUser user = (SystemUser)await _proxy.Retrieve(SystemUser.EntityLogicalName, whoAmIResponse.UserId, new ColumnSet(true));
How to update existing application code
If you want to use existing application code that was written for the CRM SDK with this library, make the following code changes.
1. Add the async keyword to methods.
2. Add await before the call to a proxy method.
3. When accessing the property of a web service response, do so as shown here.
Original CRM SDK code
_proxy.RetrieveMultiple(new QueryExpression(“account”, new Columns(true)).Entities)
(await _proxy.RetrieveMultiple(new QueryExpression(“account”, new Columns(true))).Entities)
- The sample files are not intended to be used in a production environment. You should deploy this sample to a test environment and examine it for interaction or interference with other parts of the system.
- Before you deploy this sample to a production environment, make sure that you consider the existing customizations you may have implemented in Microsoft Dynamics CRM 2013.
- This library was written by Kenichiro Nakamura at Microsoft.
Source code for the CrmSvcMobileUtil program is provided in a separate sample. Use that program to generate any custom or customized entity types in your organization for inclusion in your application.
Source Code Files
- Microsoft.Xrm.Sdk.Utility.Samples.cs - This file implementes a utility class that provides methods used by the other code files. There is no equivalent for this functionality in the CRM 2013 SDK.
For more information on Dynamics CRM authentication and app development, see Write mobile and modern apps.
download the code : View this sample online