Skip to main content

Sample console application to connect MS CRM / D365

Here is a sample console application, to get connected with MS CRM / D365
Make sure, you are using .Net 4.6 framework. In case you are using .Net Framework below 4.6, please uncomment ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; in the below code. The main purpose of this to provide more secure connectivity to the application, in which we have Microsoft updated and industry standard security policies.

For more information on this, please refer here

Steps:

1. Open Visual Studio, click on New Project
2. Select Visual C# and chose Console Application in the right pane.
3. Chose a desired name and location to the application
4. Go to Tools -> NuGet Package Manager -> Package Manager Console
5. In the console window, paste this - Install-Package Microsoft.CrmSdk.CoreTools -Version 9.0.2.6

Earlier, we used to add all the required CRM dlls from CRM SDK, now this the above NuGet package is taking care of that and it contains official SDK tools authorized by Microsoft.

Code:


using System;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;

namespace CRMConsoleApp
{
    /// <summary>
    /// Program Class
    /// </summary>
    public class Program
    {
        /// <summary>
        /// Main method
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            IOrganizationService organizationService = ConnectCRM();
            if (organizationService != null)
            {
                Guid currentUserGuid = ((WhoAmIResponse)organizationService.Execute(new WhoAmIRequest())).UserId;
                if (currentUserGuid != Guid.Empty)
                {
                    Console.WriteLine("User GUID: " + currentUserGuid.ToString());
                }
            }
        }
        /// <summary>
        /// Connecting to CRM
        /// </summary>
        /// <returns>IOrganization Service</returns>
        public static IOrganizationService ConnectCRM()
        {
            IOrganizationService organizationService = null;
            String username = "******@******.onmicrosoft.com";
            String password = "******";
            try
            {
                ClientCredentials clientCredentials = new ClientCredentials();
                clientCredentials.UserName.UserName = username;
                clientCredentials.UserName.Password = password;
                //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                organizationService = (IOrganizationService) new OrganizationServiceProxy(new Uri("https://******.crm.dynamics.com/XRMServices/2011/Organization.svc"), null, clientCredentials, null);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught - " + ex.Message);
                Console.ReadLine();
            }
            return organizationService;
        }
    }

}

Comments

Popular posts from this blog

Get old SharePoint Files using Power Automate efficiently

In this post, let us see how can we get the old files from a SharePoint Folder using Power Automate. In this example, I am considering a simple approach and this can be extended based on the requirement. To ensure the Power Automate is simple, efficient and the performance is optimized - we are not using ForEach control to loop through and identify old file or item.  We use " Send an HTTP request to SharePoint " in the Power Automate and query the data using OData filters and order by.  The idea here for the faster retrieval - selecting only a required set of fields from the file. Steps to achieve this: Login to https:make.powerautomate.com . New Cloud Flow -> Scheduled Or Instant (based on the requirement). (Configure the Start Date and Time. For repeat, every, configure the seconds/ minutes/  hours/ day/ week/ month) Click on Create Button. Click + icon to add new action. Search for SharePoint and select  " Send an HTTP request to SharePoint " Select the re...

Success confirmation on a record submission - Canvas Application

In this post let us see, how to show a successfully submitted record's data back on to the form, as a confirmation. Take two screens  Home Screen Success Screen On Home Screen: Take a Form Control on the Home Screen and map it to respective entity. In this post we are mapping it to Registrations entity. Take a Button Control and input the below expression, to create records into Registrations Entity. SubmitForm( frmRegistration ) Now, let us implement how to redirect the user to Success Message screen to successful record submission. In order to achieve this, we are going to make use of one of the Form Control's properties - OnSuccess. So, OnSuccess we are going to implement Success Screen navigation as below Navigate(' Success Screen ')   Success Screen Take a label to show - success message, as below "Registration was successful. Registration Id: " & frmRegistration .LastSubmit.'Registration Number' LastSubmit  property of a From Control helps u...

How to create a Nested Flow | Power Automate | PowerApps

In this post, let us see how to implement " Nested Flows " in Power Automate.  Some use cases: To create a common logic and make use of "Run a Child Flow" action To perform actions on the database without providing access to the end users. Let us create records in SharePoint (using Student Name and Email fields in this post)  without providing access to the end users using nested flow concept. To proceed, we need to have two flows created.  Parent Flow (which triggers the child flow) Child Flow (nested Flow) Go to flow.microsoft.com, log in and select respective environment from the drop-down. Click on "+ Create" Choose "Instant flow" Provide a name and choose " When an HTTP request is received " and "Create" Now, we need to provide inputs to the flow (inputs from the Parent Flow) We need to generate Schema for " When an HTTP request is received" trigger Click on "Use sample payload to generate schema" and...