Feb 12, 2015

Console Application skeleton for Dynamics CRM

Sometimes we need to do some operation in CRM through console applications, though not very common. Still we need to use CRM web services within the console application. CrmConnetion class can be used for that.

This code snippet can be useful in building your application.

Configuration file that keep the connection attributes;

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
<appSettings>
  <add key="connectionStr" value="Url=http://SERVERNAME/TENANTNAME; Domain=xxxxx; Username=xxxxx; Password=xxxxx;"/>
</appSettings>
</configuration>

Program code with CrmConnection;

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

using System;
using System.Configuration;
using System.ServiceModel;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Services;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace ConsoleApplication1
{
 class Program
 {
   static void Main(string[] args)
   {
     CrmConnection _crmConnection = CrmConnection.Parse(ConfigurationManager.AppSettings.Get("connectionStr"));
     OrganizationService _orgService = new OrganizationService(_crmConnection);

     try
     {
        //Logic here
     }
     catch (Exception e)
     {
         Console.WriteLine("Process failed : " + e.Message);
     }
     finally
     {
         Console.WriteLine("Process successful.");
         Console.ReadLine();
     }
   }
 }
}

No comments:

Post a Comment