Showing posts with label sample CRM application. Show all posts
Showing posts with label sample CRM application. Show all posts

Wednesday, March 21, 2018

Sample Console Application to connect to Dynamics 365

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Tooling.Connector;
using System;
using Microsoft.Xrm.Sdk.Client;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk.Messages;
using System.ServiceModel.Description;
using System.Data;
using System.ComponentModel;
using System.IO;
using Microsoft.Xrm.Client.Services;
using Microsoft.Xrm.Client;

namespace SampleConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
  //you can add the dlls from nuget package manager
                //In Solution Explorer, right - click either References or a project and select Manage NuGet Packages....
                //The Browse tab displays packages by popularity from the currently selected source
                //Select the desired version from the drop-down and select Install.Visual Studio installs the package and its dependencies into the project.You may be asked to accept license terms. When installation is complete, the added packages appear on the Installed tab. Packages are also listed in the References node of Solution Explorer

               
                // for CRM online instance
                ClientCredentials Credentials = new ClientCredentials();
                Credentials.UserName.UserName = "username";
                Credentials.UserName.Password = "password";

                string connectionString = "Url=https://domainname.crm.dynamics.com; Username=username; Password=password; authtype=Office365";
                CrmServiceClient conn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(connectionString);
                var serviceProxyPROD1 = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                var serviceProxyPRODOnline = conn.OrganizationServiceProxy;

                //try to fetch any record from CRM I am fetching case record here
                Entity incidentrecord = serviceProxyPROD1.Retrieve("incident", new Guid("A2BDAA1F-E711-E611-80F1-480FCFF4B5B1"), new Microsoft.Xrm.Sdk.Query.ColumnSet("title"));

                ////To run Action from C#
                OrganizationRequest organizationRequest = new OrganizationRequest("rollbackname");
                organizationRequest["Target"] = new EntityReference("incident", Guid.Parse("A2BDAA1F-E711-E611-80F1-480FCFF4B5B1"));
                OrganizationResponse organizationResponse = serviceProxyPRODOnline.Execute(organizationRequest);

                //Create View from Console Application
                var fetchXML = "<fetch distinct='true' mapping='logical' output-format='xml-platform' version='1.0' >";
                fetchXML += "  <entity name='incident' >";
                fetchXML += "    <attribute name='title' />";
                fetchXML += "    <attribute name='ticketnumber' />";
                fetchXML += "    <link-entity name='annotation' from='objectid' to='incidentid' alias='an'  link-type='outer'>";
                fetchXML += "      <attribute name='objectid' />";
                fetchXML += "      <filter type='and' >";
                fetchXML += "        <condition attribute='isdocument' value='0' operator='eq' />";
                fetchXML += "      </filter>";
                fetchXML += "    </link-entity>";
                fetchXML += "    <filter type='and' >";
                fetchXML += "      <condition entityname='an' attribute='objectid' operator='null' />";
                fetchXML += "    </filter>";
                fetchXML += "  </entity>";
                fetchXML += "</fetch>";

                Entity entity = new Entity("savedquery");
                entity["name"] = "Cases with Attachments";
                entity["fetchxml"] = fetchXML;
                entity["returnedtypecode"] = "incident";
                entity["querytype"] = 0;
                serviceProxyPROD1.Create(entity);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}