Before you can call CrmService Web methods ex: Create, Retrieve, RetrieveMultiple, Update, Delete and Fetch, you have to create a CrmService instance.
This tutorial will show you step by step on how to create CrmService by using CrmDiscoveryService Web service with an Internet Facing Deployment (IFD) of Microsoft Dynamics CRM.
1. You need to download Microsoft Dynamics CRM 4.0 Software Development Kit (SDK)
2. Add the CrmDiscoveryService Web Reference:
+ Option 1:
- Open Internet Explorer and enter the following URL: https://dev.crm.dynamics.com/MSCRMServices/2007/Passport/CrmDiscoveryService.asmx?WSDL
- In the File menu, click Save As.
- Name the file CrmDiscoveryService.wsdl and save it to an appropriate location on your computer, ex: C:\Users\Hoan Huynh\Desktop\sdk\wsdl.
Copy the file CrmDiscoveryService.wsdl from the sdk/wsdl folder which you just dowloaded from step 1
3. Add the Web Reference to your Visual Studio Project
- Create a C# or Visual Basic .NET project in Microsoft Visual Studio.
- In Solution Explorer, right-click References.
- Select Add Web Reference.
- In the URL field, enter the path to the CrmDiscoveryService.wsdl file: file://C:\Users\Hoan Huynh\Desktop\sdk\wsdl\crmdiscoveryservice.wsdl
- Click Go.
- Edit the Web reference name, ex: CRM. This is the name that you will put in the Using statement in the source code.
- Click Add Reference.
4. Add Assemblies
We need to add 3 Assemblies below to your project:
- Microsoft.Crm.Sdk.dll
- Microsoft.Crm.SdkTypeProxy.dll
- microsoft.crm.sdktypeproxy.xmlserializers.dll
The assemblies can be found in the SDK folder which was downloaded from step 1 (SDK\Bin or SDK\Bin\64-bit)
5. Create IFDConnection function
This step is the main part of this tutorial which will show the function to create a CrmService instace.
We will create the function in a new class file named: crm_demo.cs
And we need to using some new namespace:
[csharp] using System;using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.SdkTypeProxy;
using CRM;
[/csharp]
The function is below:
[csharp] public CrmService IFDConnectionDemo(string organization, string server, string domain, string username, string password){
// A CrmService reference.
CrmService CrmService = null;
// URL of the Web application.
string WebApplicationUrl = String.Empty;
// GUID of the user’s organization.
Guid OrganizationId = Guid.Empty;
//Remove any trailing forward slash from the end of the server URL.
server = server.TrimEnd(new char[] { ‘/’ });
// Initialize an instance of the CrmDiscoveryService Web service proxy.
CrmDiscoveryService disco = new CrmDiscoveryService();
disco.Url = "http://" + server + "/MSCRMServices/2007/SPLA/CrmDiscoveryService.asmx";
//Retrieve a list of available organizations.
RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
orgRequest.UserId = domain + "\\" + username;
orgRequest.Password = password;
RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)disco.Execute(orgRequest);
//Find the desired organization.
foreach (OrganizationDetail orgdetail in orgResponse.OrganizationDetails)
{
if (orgdetail.OrganizationName.ToLower() == organization.ToLower())
{
//Retrieve the ticket.
RetrieveCrmTicketRequest ticketRequest = new RetrieveCrmTicketRequest();
ticketRequest.OrganizationName = organization;
ticketRequest.UserId = domain + "\\" + username;
ticketRequest.Password = password;
RetrieveCrmTicketResponse ticketResponse = (RetrieveCrmTicketResponse)disco.Execute(ticketRequest);
//Create the CrmService Web service proxy.
CrmAuthenticationToken sdktoken = new CrmAuthenticationToken();
sdktoken.AuthenticationType = 2;
sdktoken.OrganizationName = organization;
sdktoken.CrmTicket = ticketResponse.CrmTicket;
CrmService = new CrmService();
CrmService.CrmAuthenticationTokenValue = sdktoken;
CrmService.Url = orgdetail.CrmServiceUrl;
WebApplicationUrl = orgdetail.WebApplicationUrl;
OrganizationId = orgdetail.OrganizationId;
break;
}
}
return CrmService;
}
[/csharp]
6. Testing the IFDConnectionDemo function
Create a new aspx page named: test_crm_service.aspx with content below:
[csharp] using System;using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.SdkTypeProxy;
using CRM;
public partial class test_crm_service : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string organization = "your-organization";
string server = "your-server:your-port-number-if-any";
string domain = "your-domain";
string username = "your-username";
string password = "your-password";
CrmService service = crm_demo.IFDConnectionDemo(organization, server, domain, username, password);
}
}
[/csharp]
That’s all, now you can call all CrmService web methods: Create, Retrieve, RetrieveMultiple, Update, Delete and Fetch
Download the source code for the tutorial includes all necessary *.dll and .wsdl file