Last time, I introduced how to start developing Mobile application for Microsoft Dynamics CRM by using CRM Mobile helper library. This time, I will introduce additional information which makes your work easier.
Previous post: Dynamics CRM Developers: Build Your Own Mobile Apps for Windows, iOS, and Android
Generate early-bound code
IntelliSense is one of the most required feature for many developers. By default Visual Studio does not provide any IntelliSense to Dynamics CRM Entities, and you need to generate classes which defines CRM Entities by yourself. Microsoft Dynamics SDK team released CrmSvcUtil.exe as part of CRM SDK, which generates such file for you. As CrmSvcUtil.exe generates code which depends on Microsoft.Xrm.Sdk.dll by default, you need extension to modify the output to use CRM Mobile helper library instead.
Follow the steps below to generate early-bound file for Mobile Application.
1. Download CRM Service Utility for Mobile Development and extract downloaded zip.
2. Open CrmSvcMobileUtil.sln file via Visual Studio from extracted folder.
3. If you will use Microsoft Dynamics CRM 2015 SDKs, change target .NET Framework to 4.5.2.
a. Right click CrmSvcMobileUtil project and click Properties.
b. Select .NET Framework 4.5.2 at Target framework dropdown.
* If you do not see .NET Framework 4.5.2, then click “install other frameworks…” and install the framework.
c. Click “Yes” at Target Framework Change dialog.
4. Right click Reference and click “Add Reference”.
5. Click “Browse” button on the bottom.
6. Add CrmSvcUtil.exe and Microsoft.Xrm.Sdk.dll from SDK.
7. Open FilteringService.cs file.
8. Update List<string> variable in GenearteEntity method. You need to specify Entity by using logical name.
9. After finish updating the list, compile the solution.
10. Open Command Prompt and change directory to CRM Service Utility for Mobile Development\C#\bin\Debug folder where compiled assembly is located.
11. Run CrmSvcUtil.exe with arguments like below.
>CrmSvcUtil.exe /codecustomization:"Microsoft.Crm.Sdk.Samples.CodeCustomizationService,CrmSvcMobileUtil" /codewriterfilter:"Microsoft.Crm.Sdk.Samples.FilteringService,CrmSvcMobileUtil" /url:ttps://<your_organization>.api.crm.dynamics.com/XRMServices/2011/Organization.svc /username: <username> /password: <password> /out: <output_filename> /namespace: <namespace>
12. Add generated file to your Mobile Application project.
How to write code with IntelliSense
Now, you are ready to write code with IntelliSense. Use the following code snippet to write your code. To use early-bound, you have to call EnableProxyTypes method for OrganizationDataWebServiceProxy.
Retrieve a record
When you retrieve a record by using Retrieve method, you need to explicitly case result to early bound. The code below retrieves an account record, and case to Account type. You can also use EntityLogicalName property of each CRM Entity classes for it’s logical name. (As Logical Name may be different from Display Name like case and incident, it is safe to use EntityLogicalName property whenever possible).
ColumnSet cols = new ColumnSet("name", "telephone1");
Account account = (Account)await _proxy.Retrieve(Account.EntityLogicalName, new Guid("<record id>."), cols);
After you cast the result, you can use IntelliSense.
Retrieve multiple records
When you retrieve multiple record by using RetrieveMultiple, you can Case results one by one by using for or foreach statement or accessing element by specifying indexer for Array or use Linq query, etc.
ColumnSet cols = new ColumnSet("name", "telephone1");
var results = await CrmHelper._proxy.RetrieveMultiple(new QueryExpression(Account.EntityLogicalName, cols));
foreach(Account account in results.Entities)
{
}
Once you obtain a result from collection, then you can use IntelliSense like below.
Create an object
You can create strong-type object by using IntelliSense by using following code.
Account account = new Account()
{
Name = "New Account",
ParentAccountId =
new EntityReference(Account.EntityLogicalName, new Guid("<id>")),
NumberOfEmployees = 10
};
As each property has Type, if you try to assign different type, you see error message like below.
Another way to cast Entity
You can also use ToEntity<T>() method of Entity class.
ColumnSet cols = new ColumnSet("name", "telephone1");
Entity result = await _proxy.Retrieve(Account.EntityLogicalName, new Guid("<record id>."), cols);
Account account = result.ToEntity<Account>();
What’s Next?
We have published sample application as open source, which utilize this technic. Please download the sample application and see how it uses early-bound programming model.
CRM to Go – Sample app for Dynamics CRM
This sample application does not utilize MVVM model, so that it’s easy to debug and follow the code in code behind of UI. If you prefer MVVM model sample, please refer to samples below. (These samples use CRM Mobile Helper, but do not use early-bound programming model)
Activity Tracker Sample app for Dynamics CRM
Activity Tracker Plus Sample app for Dynamics CRM
Ken
Premier Mission Critical/Premier Field Engineer
Microsoft Japan