Dynamcis CRM On Premise Console App

1 Comment

To create a console app that connects to Dynamics CRM 2015, we will use the CrmServiceClient.

Create a new Visual Studio project and add the assemblies:

  • Microsoft.Crm.Sdk.Proxy
  • Microsoft.Xrm.Sdk;
  • Microsoft.Xrm.Tooling.Connector

In the code, add Using:

using Microsoft.Xrm.Tooling.Connector;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;

To connect to CRM, we will use the connection information to pass to CrmServiceClient:

  string userid = "Administrator";
 string password = "yourpassword";
 string domain = "YOURDOMAIN";
 string orgname = "CarlCo";
 string server = "localhost";
 string port = "5555";
 
 CrmServiceClient conn = new CrmServiceClient(new System.Net.NetworkCredential(userid, password, domain),server, port, orgname);

We can then use a QueryExpression to get data from CRM with the Organization Service:

 IOrganizationService _orgService;
 _orgService = (IOrganizationService)conn.OrganizationServiceProxy;

 QueryExpression query = new QueryExpression("contact");
 query.ColumnSet.AddColumns("firstname", "lastname");

 EntityCollection result1 = _orgService.RetrieveMultiple(query);
 foreach (var a in result1.Entities)
 {
    Console.WriteLine("Name: " + a.Attributes["firstname"] + " " + a.Attributes["lastname"]);
 }
 Console.Read();

Code can now be run:

Full code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Xrm.Tooling.Connector;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;

namespace Carl.Crm.ConsoleApp
{
 class Program
 {
 static void Main(string[] args)
 {

 string userid = "Administrator";
 string password = "yourpassword";
 string domain = "YOURDOMAIN";
 string orgname = "CarlCo";
 string server = "localhost";
 string port = "5555";
 
 CrmServiceClient conn = new CrmServiceClient(new System.Net.NetworkCredential(userid, password, domain),server, port, orgname);

 IOrganizationService _orgService;
 _orgService = (IOrganizationService)conn.OrganizationServiceProxy;

 QueryExpression query = new QueryExpression("contact");
 query.ColumnSet.AddColumns("firstname", "lastname");

 EntityCollection result1 = _orgService.RetrieveMultiple(query);
 foreach (var a in result1.Entities)
 {
 Console.WriteLine("Name: " + a.Attributes["firstname"] + " " + a.Attributes["lastname"]);
 }
 Console.Read();
 }
 }
}
THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

One Response to Dynamcis CRM On Premise Console App

  1. hello,
    i have been using crm 2016 onpremise version. we have wcf services which are connected to crm database. while creating or accessing data from crm, an app pool user is used. these wcf services are called from other web applications. is there any way to authenticate user who is calling the wcf service?

Leave a Reply

Your email address will not be published. Required fields are marked *