step 1: Create a new Windows Form in Visual Studio 2010.
step 2: Add two button controls , one for discover the organisations name and another for connect to organisation.Again, add a List box control to display the organisations name.
step 3: Now Right click on References on the Windows Form project and select Add Reference from the menu.
step 4: Add a reference to the Windows Form project for System.Runtime.Serialization.dll and System.ServiceModel.dll
step 5: In the Add Reference dialog select the Browse tab.
step 6: Browse to the ..\SDK\Bin folder and select Microsoft.Xrm.Sdk.dll and Microsoft.Crm.Sdk.Proxy.dll.
step 7: Right click on your Project on the Solution explorer and select
Add -> Add Existing item
step 8: Browse to the ..\SDK\samplecode\cs\helpercode & select deviceidmanager.cs.
step 9:In the code behind add the following using statements for the SDK.
using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk.Discovery; using System.ServiceModel.Description; using Microsoft.Crm.Sdk.Messages;
step 10: Add the following class scoped (i.e inside -> public partial class Form1 : Form) private properties to hold references to the Organization Details, the Discovery Service and the Organization Service. These will be populated when we connect.
private OrganizationDetail CurrentOrganizationDetail { get; set; } private IDiscoveryService DiscoveryService { get; set; } private IOrganizationService OrgService { get; set; }
step 11: When the user inputs the server name, you will need to build the full Uri to the discovery service. Add the following helper method to the class Form1 to build the Uri.
public Uri GetDiscoveryServiceUri(string serverName) { string discoSuffix = @"/XRMServices/2011/Discovery.svc"; return new Uri(string.Format("{0}{1}", serverName, discoSuffix)); }
step 12: In the new event handler for the Discover button add the following code.
//Online CRM 2011 server url. Uri myuri = new Uri("https://(Online CRM 2011 server url..)/XRMServices/2011/Discovery.svc"); //get device credential var deviceCrednt = new ClientCredentials(); deviceCrednt = GetDeviceCredentials(); // give online crm2011 credential var creds = new ClientCredentials(); creds.UserName.UserName = "your online crm 2011 username"; creds.UserName.Password = "your online crm 2011 password"; DiscoveryServiceProxy dsp = new DiscoveryServiceProxy(myuri, null, creds, deviceCrednt); //Use a RetrieveOrganizationsRequest to retrieve the available organization names RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest(); //The retrieve above will return a RetrieveOrganizationResponse that //has a property OrganizationDetails that is an array of OrganizationDetail objects RetrieveOrganizationsResponse orgResponse = dsp.Execute(orgRequest) as RetrieveOrganizationsResponse; Cursor.Current = previousCursor; //Add the following code to bind the list to the list box for display. ltbOrg.DisplayMember = "FriendlyName"; ltbOrg.DataSource = orgResponse.Details; step 13: Add defination for GetDeviceCredentials method. protected virtual ClientCredentials GetDeviceCredentials() { return Microsoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice(); } step 14:In the new event handler for the Connect button add the following code. //to ensure that the user has selected an organization before proceeding if (ltbOrg.SelectedItem == null) { MessageBox.Show("You must select an organization before connecting"); return; } //Set the CurrentOrganizationDetail property from the selected item in the list box. this.CurrentOrganizationDetail = ltbOrg.SelectedItem as OrganizationDetail; //to interact with the CRM server. Uri orgServiceUri = new Uri(CurrentOrganizationDetail.Endpoints[EndpointType.OrganizationService]); IServiceConfiguration orgConfigInfo = ServiceConfigurationFactory.CreateConfiguration(orgServiceUri); var deviceCrednt = new ClientCredentials(); deviceCrednt = GetDeviceCredentials(); var creds = new ClientCredentials(); creds.UserName.UserName = "CRM 2011 username"; creds.UserName.Password = "CRM 2011 password"; OrgService = new OrganizationServiceProxy(orgServiceUri, null, creds, deviceCrednt); //Create a WhoAmIRequest and use the new organization service instance to call to the server and //find out the current user id. WhoAmIRequest req = new WhoAmIRequest(); var response = OrgService.Execute(req) as WhoAmIResponse; MessageBox.Show("You are connected Successfully");