Connect to Dynamics 365 using C# SDK

Hi,

I am going to show  in a few steps how to create an organization service proxy for dynamics 365 .

Steps :

  • Create a new project in visual studio
  • Add nuget package :"Microsoft.CrmSdk.CoreAssemblies"
  • Add the following piece of code

CrmServiceClient conn = new CrmServiceClient("Url=https://moqcrm20.crm6.dynamics.com/; Username=test; Password=test; authtype=Office365");

// Cast the proxy client to the IOrganizationService interface.
var _orgService = (Microsoft.Xrm.Sdk.IOrganizationService)conn.OrganizationWebProxyClient != null ? (Microsoft.Xrm.Sdk.IOrganizationService)conn.OrganizationWebProxyClient : (Microsoft.Xrm.Sdk.IOrganizationService)conn.OrganizationServiceProxy;

How To : Obtain Records Count Without Retrieving Rows of Data in CRM Using C# SDK

Brief:

Let's suppose we have a scenario that requires to get only records count of the entity "Account".

Code:

string fetchQuery = @"<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='entity name'>
<attribute name='attribute name' aggregate='count' alias='aliasName'/>
</entity>
</fetch>";

EntityCollection value = _serviceProxy.Execute(new FetchExpression(fetchQuery));

              Returned result will be something as follow:

<resultset morerecords="0"'>
<result>
<aliasName>20</aliasName>
</result>
</resultset>

Note: you can check how to create  Service Proxy at the following link

Quick View Form and sub grid Issue in Dynamics 365

Hi,

Intro :

I encountered a problem while working with quick view forms having sub grids.

The quick view form is meant to display the account details upon selecting an account on lookup in custom entity form.

Problem:

Quick view form has sub grid displaying related record to account such as cases and opportunities , the problem occurs whenever you change the select account , all the details are updated to reflect the new selected account except the sub grids .

 They keep displaying the related records to the old one.

Fix :

  1. Add on change event handler on the account lookup
  2. Include the following piece of code in your java script function

var quickViewControl = Xrm.Page.ui.quickForms.get("your quick view form name");
if (quickViewControl != undefined) {
if (quickViewControl.isLoaded()) {
quickViewControl.refresh();
}
}

Export Translations in Dynamics CRM Progrmatillcally

SDKThe following code illustrates how to export Translation file from CRM problematically:

ExportTranslationRequest req = new ExportTranslationRequest();
req.SolutionName = "Final Solution";
ExportTranslationResponse resp = (ExportTranslationResponse)service.Execute(req);
String outputDir = @"C:\CRMTranslaions\";
byte[] exportXml = resp.ExportTranslationFile;
string filename = "CRMTranslations" + ".zip";
File.WriteAllBytes(outputDir + filename, exportXml);
Console.WriteLine("Translations exported to {0}.", outputDir + filename);

Could not load file or assembly ‘Microsoft.Xrm.Client, Version=5.0.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35′ or one of its dependencies. The system cannot find the file specified.

To fix this error, do the following steps.

  • copy the file from the following folder "SDK\Bin"
  • Paste if in the following folder "program files\Microsoft Dynamics CRM\CRMWeb\bin"

Note: you can download the SDK  from the following link
http://www.microsoft.com/en-us/download/details.aspx?id=24004

Disable Caching for Organization Service In CRM 2011/2013/2015

MS CRM has caching feature but sometimes you want to disable it to get the latest updates,two ways to disable caching
First way - Configuration

Add the following configuration block to the config file of your application

<configSections>

<sectionname="microsoft.xrm.client"type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client"/>

</configSections>

<microsoft.xrm.client>

<contexts>

<addname="Xrm"type="Xrm.XrmServiceContext, Xrm"serviceName="Xrm"/>

</contexts>

<services>

<addname="Xrm"type="Microsoft.Xrm.Client.Services.OrganizationService, Microsoft.Xrm.Client"/>

</services>

</microsoft.xrm.client>
Second Way - Pragmatically

Microsoft.Xrm.Client.Caching.ObjectCacheManager.GetInstance().Clear();

How to solve : A proxy type with the name account has been defined by another assembly

The problem is actually with WCF attempting to deserialize the server response and not being able to identify the correct type. The best method to sort this issue is to pass in the current assembly using Assembly.GetExecutingAssembly() to the ProxyTypesBehavior() while creating the proxy like so

using (serviceProxy = new OrganizationServiceProxy(config.OrganizationUri,
config.HomeRealmUri,
config.Credentials,
config.DeviceCredentials))
{
// This statement is required to enable early-bound type support.
serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior(Assembly.GetExecutingAssembly()));
}