by Chuck Oldes, MCT, MCTS, MCSE
21. May 2012 08:05
Many times documentation regarding how to use certain code assumes that you have a the level of knowledge required to jump right into the process. Lets assume for a minute that your new to development or you haven’t attempted to write code for some time, you might need a refresher on how to setup Visual Studio. For the purpose of this blog entry I am going to create a simple console application that confirms the Organization in a on premise deployment of CRM 2011.
This same process could be used to address a IFD or MS CRM Online deployment with some simple adjustments. We do have to make some assumptions regarding the preparation steps that have been taken, first that you have a copy of Visual Studio 2010, an operational MS CRM on premise deployment and a downloaded copy of the MS CRM 2011 SDK.
Start by launching Visual Studio 2010. Then click File > New> Project.
Click “Console Application”. Assign a name to the new console application.
One of those adjustments that will cause you to curse at your monitor and send you in search of a blog for an answer is making sure you set the Target framework. Read more about this setting
To expose the required assemblies for this application ensure that the Target framework is set to .Net Framework 4. Right click the application name and click Properties.
Select the Target framework dropdown, select .NET Framework 4.
Now we need to add two helper class files from the MS CRM 2011 SDK. Click ADD > Existing Item.
Navigate to the location where you installed the SDK. \CRM2011SDK\sdk\samplecode\cs\helpercode Locate crmservicehelper.cs and devicemanager.cs files and click “Add”. Both of these files are provided by Microsoft to assist in communicating with MS CRM 2011 web services.
Notice that helper classes have been added to your project.
These helper classes have a relationship, where the crmserviceclass.cs instantiates deviceidmanager.cs . So I suggest you start with this file. Open the file by clicking on it. You will note that ServiceModel.Description is not instantiated by not being referenced.
To add a reference, right click on References > Add Reference.
Add required assemblies to the solution.
System.ServiceModel – Read more about the namespace
System.Runtime.Serialization – Read more about the namespace
System.Security – Read more about the namespace
Make sure your on the .NET tab, locate each of the assemblies and click “Ok”.
Once added you should notice that the Intellisense in Visual Studio has removed the red underline.
Now open crmservicehelper.cs and you will notice some other assemblies missing. Follow the same procedure to locate the missing assembly. Ensure that System.DirectoryServices.AccountManagement. Read more about the namespace
Select the assembly and click “Ok”.
Microsoft Sdk provides a number of assemblies that are useful. Right click on References > Add Reference. Change to the Browse tab, using the Look in: dropdown find CRM2011SDK\sdk\bin to locate the collection of assemblies made available by Microsoft. Select microsoft.xrm.sdk.dll and click “Ok” .
This blog does not delve into the code required to make the call to the Discovery web service, rather to focus how to setup Visual Studio to correctly execute the method. To obtain additional information related to the structure of the code used, Refer to MS Course 80295A
String discourl = "http://servername:port/XRMServices/2011/Discovery.svc";
Uri dInfo = new Uri(discourl);
ClientCredentials clientcred = new ClientCredentials();
DiscoveryServiceProxy dsp = new DiscoveryServiceProxy(dInfo, null, clientcred, null);
dsp.Authenticate();
RetrieveOrganizationsRequest rosreq = new RetrieveOrganizationsRequest();
RetrieveOrganizationsResponse r = (RetrieveOrganizationsResponse)dsp.Execute(rosreq);
OrganizationDetail orginfo = null;
foreach (OrganizationDetail o in r.Details)
{
Console.WriteLine(o.FriendlyName);
}
Console.ReadLine();
Copy and paste the code above into the program.cs file and provide the name of your server. You will notice that Intellisense is flagging a number of calls that appear not to be working.
Notice that a number of the new reference namespace are not defined. Right click on the first underlined code and right click. Click Resolve, select to add the namespace with a using statement.
Repeat this process until all errors are removed.
Those assemblies have now been instantiated in the program.cs file.
At this point the sample code should run, but first Build the Solution. Make sure it is error free and then press F5.
A list of Organizations on the CRM platform should be returned. See the example below.