Connect to CRM Online 2011

1. VS will create the following solution. Remove Workflow project and a reference to it.

Here is what should be obtained as a result:

2. Add a new plug-in and register it on the particular message:

CRM Plud-in Unit Testing Procedure

Follow the steps below:
1. Open CRM Explorer

2. Expand Entities tree-node, right click on Customer entity and select Create Plug-in.

 

3. VS will display the following:

 

Set Post Operation in Pipeline Stage, then select Update message and click OK. VS will create PostCustomerUpdate class and register a new step in CRM on Update message. Every time the Customer is updated PostCustomerUpdate plug-in will be triggered.

4. Add new folders DAL and BL to Plugins project. Create two files: CustomerDao.cs and CustomerBl.cs and place them into DAL and BL folders respectively.

Make CustomerBl and CustomerDao classes public. Add interface named ICustomerDao to DAL folder and implement it into CustomerDao.

5. Check whether references to Microsoft.Xrm.Sdk and Microsoft.Crm.Sdk.Proxy exist. If not, add them from the SDK\bin folder.

6. Make changes in ExecutePostCustomerUpdate method to get Customer entity, create instance of CustomerBl and call method UpdateCustomer in PostCustomerUpdate as it follows:

// note: your custom Plug-in business logic.
Contact customer;
if (localContext.PluginExecutionContext.InputParameters != null &&
localContext.PluginExecutionContext.InputParameters.Contains("Target") &&
localContext.PluginExecutionContext.InputParameters["Target"] is Entity)
{
var entity = В В В (Entity)localContext.PluginExecutionContext.InputParameters["Target"];
if (!entity.LogicalName.Equals(Contact.EntityLogicalName))
throw new ArgumentException("Plugin execution failure. Can not retrieve required entity.");
customer = entity.ToEntity<Contact>();
}
else
{
throw new ArgumentException("Plugin execution failure. Can not retrieve required entity.");
}
var manager = new CustomerBl(new CustomerDao(), customer);
manager.OnUpdateCustomer();

7. Generate the wrapper object using SDK utility crmsvcutil /nologo /language:CS /url:https://developmentplatform.crm4.dynamics.com/XRMServices/2011/Organization.svc /out:wt_entities.cs /username:somebody@microsoft.com /password:verystrongpassw0rd /serviceContextName:PluginOrgContext. Add this file to Plugin project.

8. Add property PluginOrgContext PluginContext to inner LocalPluginContext class and instantiate it in the constructor.

internal PluginOrgContext PluginContext { get; private set; }

…

this.PluginContext = new PluginOrgContext(OrganizationService);

9. Add code to CustomerBl.cs:

private ICustomerDao _dao;
private Contact _customer;
public CustomerBl(ICustomerDao dao, Contact contact)
{
_dao = dao;
_customer = contact;
}

10. Change method

public void OnUpdateCustomer()
{
throw new NotImplementedException();
}

To

public void OnUpdateCustomer()
{
throw new InvalidPluginExecutionException("Customer Updated!");
}

11. Sign Plug-in project with the key and rebuild the solution.

12. Publish newly-created plug-in to CRM Online:

You can also use a Plug-in registration tool.

13. Go to CRM Screen, open Customer to edit, make changes and click вЂ?Save’ button on the Ribbon. The error message should appear:

It indicates that plug-in works correctly. Notice that each time the Customer is updated, plug-in is triggered. Therefore, remove a throwing exception, rebuild the solution and deploy it again. Run and check if everything works fine.

14. Now, we’re going to create the first test. Right click on Plug-ins project and select вЂ?Run Pex’

Pex will start the exploration. The result of exploration should be as follows:

You can see Pex Explorer view. Here you can view information about methods and generated tests, including passed/failed tests reports.

Insert math as
Block
Inline
Additional settings
Formula color
Text color
#333333
Type math using LaTeX
Preview
\({}\)
Nothing to preview
Insert