The Microsoft.Xrm.Sdk.dll library provided as part of the Dynamics CRM 2011 SDK makes it easy to write .NET applications that target the desktop. This library has dependencies on .NET 4.0 and Windows Identity Foundation (WIF), neither of which are available for Windows Phone and Windows 8 RT platforms. Although the WS-Trust protocol used by Dynamics CRM 2011 for authentication is fairly well defined and several folks have built implementations for other languages/platforms, we want to make it easy for you to build amazing applications on these platforms , so a reference implementation of the CRM SDK for these platforms is provided here. Unlike the desktop version, where we only provided you with the binary assembly, we have provided the complete source code, so you should be able to make changes to fit your specific needs. Please note that this works for Dynamics CRM Online deployments, both Office 365 and Windows Live ID, and for Internet Facing Deployments (IFD). It does not work for Dynamics CRM on-premises deployments using Active Directory authentication.
This code is provided “as is”, so you are responsible for making any fixes.
The rest of this post describes how to use the code to authenticate and send web service requests.
Authenticating to the CRM Web Services
Use ServiceConfigurationFactory to construct a DiscoveryServiceConfiguration or an OrganizationServiceConfiguration object. These classes load the relevant metadata and raise one of these events:
- MetadataLoaded event if the service metadata has been loaded successfully
- MetadataLoadFailure for other failure cases, such as when the endpoint is not reachable
Once the metadata has been loaded, you can call the Authenticate method. On completion, the Authenticatemethods raise the SignInComplete event. The SignOnCompletedEventArgs object on the event callback contains the returned token in the Result property on successful authentication, and contains a non-null exception in the Error property in the case of failed authentication. You can use the returned token to call CRM web service methods.
Windlows Live ID authentication requires device authentication to be performed first, in which case the authentication flow is AuthenticateDevice -> DeviceSigninComplete event -> Authenticate ->SignInComplete event, which can be achieved as shown below:
- Attach to the DeviceSigninComplete event and do the following in the event handler:
- Handle any error in device authentication by examining the SignOnCompletedEventArgs.Errorproperty. If there is no error, continue with the following
- Attach to the SignInComplete event for the user authentication. In the handler, handle any error/read the returned token
- Call Authenticate(ClientCredentials clientCredentials, RequestSecurityTokenResponsedeviceSecurityToken) passing in the user credentials and the returned token response from device authentication
- Call AuthenticateDevice passing in the device credentials to trigger the authentication flow. TheDeviceSigninComplete event triggers the user authentication (achieved by step 1)
Calling the CRM Web Services
- If no cached token available, authenticate as shown above, and cache the returned token.
- Instantiate either the DiscoveryServiceProxy or the OrganizationServiceProxy object as needed.
- Set the SecurityTokenReponse property on the proxy object to the cached token.
- Attach to the corresponding completion event for the method to be called. For example, useCreateCompleted for the Create method, ExecuteCompleted for the Execute method.
- Call the web service method on the DiscoveryServiceProxy or OrganizationServiceProxy objects.