Sunday 29 January 2012

Create a Crm console app from scratch.

*Set the project's Target framework to be .Net Framework 4.

*To interact with Crm 2011 API, you can either refer to CRM 2011 sdk binary files or Service Reference
+ If you want to use sdk binary files: Include the
Microsoft.Crm.Sdk.Proxy.dll
Microsoft.Xrm.Sdk.dll in project reference
System.Security
System.Runtime.Serialization
and System.ServiceModel (This is for using WCF)

+ If you want to use Service Reference: Download the Organization Service endpoint from the Crm Developer Resources page.

*Reference the namespaces in the code.
using System.ServiceModel; // For using the WCF
using System.ServiceModel.Description; // You need this to use the ClientCredential class.
//Contains enumerations of possible picklists and integer values for some attributes.
//The naming convention of the classes is to make it easier to locate the specific attribute.
using Microsoft.Crm.Sdk;
//Defines the data contracts for attribute types, interfaces for authoring plug-ins,
//and other general purpose xRM types and methods.
using Microsoft.Xrm.Sdk;
//Defines classes for use by client-code, including a data context,
//proxy classes to ease the connection to Microsoft Dynamics CRM, and the LINQ provider.
using Microsoft.Xrm.Sdk.Client;
// Defines request/response classes for Create, Retrieve, Update, Delete, Associate , Disassociate, and the metadata classes.
using Microsoft.Xrm.Sdk.Messages;
// Defines query classes required to connect to Microsoft Dynamics CRM.
using Microsoft.Xrm.Sdk.Query;
Microsoft.Crm.Sdk.Messages;

The namespaces required for doing a WhoAmIRequest are Microsoft.Crm.Sdk, Microsoft.Xrm.Sdk.Client, Microsoft.Crm.Sdk.Messages, System.ServiceModel, System.ServiceModel.Description.

* Include the deviceidmanager.cs file in the project for authentication. Your can find the class in the crm sdk helper folder \\SDK CRM 2011\sdk\samplecode\cs\helpercode\deviceidmanager.cs.
+ Also include the Microsoft.Crm.Services.Utility namespace.

* Setup the credential which will be used to connect to Crm WCF endpoint

* Connect to WCF endpoint

* Require early bound type support

* Do something with theh endpoint


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel; // For using the WCF
using System.ServiceModel.Description;
// Contains enumerations of possible picklists and integer values for some attributes. 
// The naming convention of the classes is [open arrow brace]entityname[close arrow brace][open arrow brace]attributename[close arrow brace] to make it easier to locate the specific attribute.
using Microsoft.Crm.Sdk;
// Defines the data contracts for attribute types, interfaces for authoring plug-ins, 
// and other general purpose xRM types and methods.
//using Microsoft.Xrm.Sdk;
// Defines classes for use by client-code, including a data context, 
// proxy classes to ease the connection to Microsoft Dynamics CRM, and the LINQ provider.
using Microsoft.Xrm.Sdk.Client;
// Defines request/response classes for Create, Retrieve, Update, Delete, Associate , Disassociate, and the metadata classes.
//using Microsoft.Xrm.Sdk.Messages;
// Defines query classes required to connect to Microsoft Dynamics CRM.
//using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Services.Utility;
using Microsoft.Crm.Sdk.Messages;


namespace CrmConsoleApplication1
{
    class Program
    {
        private static OrganizationServiceProxy _serviceProxy;
        private static ClientCredentials _clientCreds;
        //private static ClientCredentials _deviceCreds;

        static void Main(string[] args)
        {
            // Setup credential
            _clientCreds = new ClientCredentials();
            _clientCreds.Windows.ClientCredential.UserName = "Administrator";
            _clientCreds.Windows.ClientCredential.Password = "NotTheRealPassword";
            _clientCreds.Windows.ClientCredential.Domain = "PLAYGROUND";
            // The DeviceIdManager class registers a computing device with Windows Live ID, through the generation of a device ID and password, 
            // and optionally stores that information in an encrypted format on the local disk for later reuse.
            // This will fail here as this vm don't have access to the Internet therefore there is no way for it to generate a device ID with login.live.com
            //_deviceCreds = DeviceIdManager.LoadOrRegisterDevice();

            // Connect to Crm WCF endpoint
            using (_serviceProxy = new OrganizationServiceProxy(new Uri("http://localhost:5555/CRM2011RTM/XRMServices/2011/Organization.svc"),
                                                                null,
                                                                _clientCreds,
                                                                null))
            {
                // require early bound type support
                _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

                OrganizationServiceContext orgContext = new OrganizationServiceContext(_serviceProxy);
                WhoAmIRequest request = new WhoAmIRequest();
                WhoAmIResponse response = (WhoAmIResponse)orgContext.Execute(request);
                Console.WriteLine("Your Crm user Guid is {0}", response.UserId);
            }

            Console.Read();
        }
    }
}

1 comment:

  1. This is documented in the SDK under sdk\walkthroughs\portal\consoleappwalkthrough\

    You should also really look into the Microsoft.Xrm.Client assembly, especially the XrmServiceContext, CrmConnection, and the ConnectionDialog. These additional connection classes support all deployment types of CRM (AD, Claims, Live, ODSP).

    ReplyDelete