Saturday 4 February 2012

Create a Crm console app from scratch 2. A quick startup program

When writing an application that uses the Microsoft Dynamics CRM SDK, you typically need to perform the following steps to configure your application’s project.

In the project’s properties, set the target framework to .NET Framework 4.

Add the following .NET references to your project:

System.Data.Linq

System.Runtime.Serialization

System.Security

System.ServiceModel

System.DirectoryServices.AccountManagement


Add the required Microsoft Dynamics CRM SDK assembly references. At a minimum, add Microsoft.Crm.Sdk.Proxy and Microsoft.Xrm.Sdk. For a complete list of the assemblies included in the Microsoft Dynamics CRM SDK, see Assemblies Included in the Microsoft Dynamics CRM SDK.


In most cases, you need to install Windows Identity Foundation and add a reference to Microsoft.IdentityModel to your project.

Including the following namespaces
using System;
//using System.ServiceModel; // Maybe needed for more complex crm programs
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Client;
//using Microsoft.Crm.Sdk; // Maybe needed for more complex crm programs
using Microsoft.Crm.Sdk.Messages;

using System;
//using System.Collections.Generic;
//using System.Text;
//using System.ServiceModel;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Client;
//using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Messages;

namespace CrmConsoleApplication2
{
    public class Program
    {
        private static ClientCredentials _clientCreds;
        private static OrganizationServiceProxy _serviceProxy;

        public static void Main(string[] args)
        {
            _clientCreds = new ClientCredentials();
            _clientCreds.Windows.ClientCredential.UserName = "Administrator";
            _clientCreds.Windows.ClientCredential.Password = "NotTheRealPassword";
            _clientCreds.Windows.ClientCredential.Domain = "PLAYGROUND";

            using (_serviceProxy = new OrganizationServiceProxy(new Uri("http://localhost:5555/CRM2011RTM/XRMServices/2011/Organization.svc")
                , null
                , _clientCreds
                , null))
            {
                WhoAmIRequest request = new WhoAmIRequest();
                WhoAmIResponse response = (WhoAmIResponse)_serviceProxy.Execute(request);
                Console.WriteLine("User {0} has just connected", response.UserId);
            }

            Console.ReadLine();
        }
    }
}

#Use the Sample and Helper Code

No comments:

Post a Comment