Tuesday, March 6, 2012

Web Services in CRM 4.0.


-Web Services Fundamentals

Microsoft Dynamics CRM uses the Service Oriented Architecture (SOA) concept, which implements a series of Web Services to provide extensibility support.

Web Services are language- and platform-independent, so basically they can be consumed by an application written in Java, Visual Basic, or C#. They also can be used on a Microsoft Windows–based OS, as well as UNIX and Macintosh platforms.

The Microsoft CRM Web Services are divided and defined into three main groups:
Discovery Web Service
Main Data Web Service
Metadata Web Service

To easily see these Web Services' definitions and get the URL addresses of the Web Services Description Language (WSDL) documents within Microsoft CRM, follow these steps:

1. Go to Settings, Customizations.

2. Select the option Download Web Services Description Files (see Figure

As you can see in you can download the definitions for two main Web Services—the Main Data and the Metadata Web Services. The Discovery Web Service is not shown because it is not organization-dependent.
To understand how to use these Web Services, you need to create a new console application in C# to follow the examples of this chapter. To do so, open Visual Studio 2005 and go to File, New, Project. From the project types, go to Visual C#/Windows and select the Console Application on the right side where the templates are.If You are using 2010 then  you may need to use the compatibility feature to add a Web Reference (instead of a Service Reference):Right click on the project and choose "Add Service Reference"
1. Click on the "Advanced..." button
2. Click on the "Add Web Reference..." button
3. Paste your URL in the URL box, e.g., http://MyServer/_vti_bin/excelservice.asmx
4. Click on the "Add Reference" button

The Web Services definition files are explained next.


Discovery Web Service
As we explained previously, one of the new features of Microsoft CRM is the multitenancy capability, which is support for more than one organization on the same server. In previous versions, the Microsoft CRM product was limited to only one Organization per server.
Because of that, you need to query a general Web Service called CRMDiscoveryService that will give you the right Web Service for the organization you want to work with. You can find the query by navigating here:

Code View:
http://<servername>:<portnumber>/MSCRMServices/2007/AD/CrmDiscoveryService.asmx?WSDL

For example:

http://crm4:5555/MSCRMServices/2007/AD/CrmDiscoveryService.asmx?WSDL

To add a Web Reference for this Web Service go to Visual Studio 2005, and using the new console application project you created, right-click the project name in the Solution Explorer and choose Add Web Reference from the menu. Enter the URL of the discovery service, click GO, and then enter CrmSdk.Discovery as the Web Reference Name
Click Add Reference to finally add the Web Service reference to your project.
Note:If u are using vs 2010 Then u have Follow following step to Add web Reference.
you may need to use the compatibility feature to add a Web Reference (instead of a Service Reference):Right click on the project and choose "Add Service Reference"
5. Click on the "Advanced..." button
6. Click on the "Add Web Reference..." button
7. Paste your URL in the URL box, e.g., http://MyServer/_vti_bin/excelservice.asmx
Click on the "Add Reference" button

Note
Visual Studio must be run from a computer connected to the same domain and LAN as the CRM Server for all the samples in this chapter or you will be prompted to enter the Windows credentials when adding the Web Service reference.

By querying this Web Service, you can retrieve all the organizations that a specific user belongs to and get the right Web Service location. An example of this follows:

Code View:
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(GetCrmServiceForOrganization("ORGNAME"));
            Console.ReadKey();
        }

        private static string GetCrmServiceForOrganization(string organizationName)
        {
            string urlResult = "";
            CrmSdk.Discovery.CrmDiscoveryService myCrm = new CrmSdk.Discovery.CrmDiscoveryService();
            myCrm.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            CrmSdk.Discovery.RetrieveOrganizationsRequest myRequest = new CrmSdk.Discovery.RetrieveOrganizationsRequest();
            CrmSdk.Discovery.RetrieveOrganizationsResponse myResponse = (CrmSdk.Discovery.RetrieveOrganizationsResponse)myCrm.Execute (myRequest); foreach (CrmSdk.Discovery.OrganizationDetail tDetail in myResponse.OrganizationDetails)
            {
                Console.WriteLine("Organization = " + tDetail.OrganizationName);
                if (String.Compare(tDetail.OrganizationName, organizationName, true) ==0)
                {
                    return tDetail.CrmServiceUrl;
                }
            }
            return urlResult;
        }
    }
}

If your CRM Site is not Configured in your  local machine than you have to specify the UserName,Password and Domain Name.So you have to replace the Line
myCrm.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; with below Four Lines.

 System.Net.CredentialCache.DefaultNetworkCredentials.UserName = "username";
 System.Net.CredentialCache.DefaultNetworkCredentials.Domain = "server";
 System.Net.CredentialCache.DefaultNetworkCredentials.Password = "password";
 myCrm.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            
Note

Remember to always replace ORGNAME with your organization name.
                        

This function illustrates how to get the correct Web Service address to use for the organization you want to work with.

Main Data Service and Meta Data Service will be Discuss in my Next Post.

No comments:

Post a Comment