Use CrmServiceClient to execute web request against Web API – Dynamics 365


In the previous post we saw how to use CrmServiceClient to connect to CDS using Authentication Type – OAuth and execute web request using Organization.svc service

https://nishantrana.me/2020/11/09/sample-code-to-connect-to-cds-dynamics-365-ce-using-oauth/

Here we will extend the same example to execute web request using Web API.

  • Create the contact record with first name and last name populated

using Microsoft.Xrm.Tooling.Connector;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;

namespace SampleConsoleApp
{
class Program
{
static void Main(string[] args)
{
string ConnectionString = "AuthType = OAuth; " +
"Username = [username]@[domain].onmicrosoft.com;" +
"Password = [password]; " +
"Url = https://[orgname].crm.dynamics.com;" +
"AppId=51f81489-12ee-4a9e-aaae-a2591f45987d;" +
"RedirectUri=app://58145B91-0C36-4500-8554-080854F2AC97;" +
"LoginPrompt=Auto";

CrmServiceClient svc = new CrmServiceClient(ConnectionString);

// specify OData Headers
Dictionary<string, List<string>> odataHeaders = new Dictionary<string, List<string>>
{
{ "Accept", new List<string>() { "application/json" } },
{ "OData-MaxVersion", new List<string>() { "4.0" } },
{ "OData-Version", new List<string>() { "4.0" } }
};

if (svc.IsReady)
{
// create a contact record with firstname and lastname populated
dynamic contact = new JObject();
contact.firstname = "Meeska";
contact.lastname = "Mooska";
string jsonContact = Newtonsoft.Json.JsonConvert.SerializeObject(contact);

// create the contact record
// Parameters - HttpMethod, QueryString, Body, Customer Headers, Content Type
HttpResponseMessage httpResponse = svc.ExecuteCrmWebRequest(
HttpMethod.Post,
"contacts",
jsonContact,
odataHeaders,
"application/json");

if (httpResponse.IsSuccessStatusCode)
{
var contactUri = httpResponse.Headers.GetValues("OData-EntityId").FirstOrDefault();
Console.WriteLine("Contact URI: {0}", contactUri);
}
else
{
Console.WriteLine(httpResponse.ReasonPhrase);
}
}
}
}
}

  • Retrieve first name and last name for all the contact

// retrieve first name and last name of all the contact records 
HttpResponseMessage httpResponse = svc.ExecuteCrmWebRequest(
HttpMethod.Get,
"contacts?$select=firstname,lastname",
string.Empty,
odataHeaders,
"application/json");

 

Get the details here – ExecuteCrmWebRequest

Hope it helps..

Process Optimization – Dynamics 365 CE / CRM / CDS SSIS Integration toolkit – KingswaySoft


Process Optimizations is a new feature added in the CDS / CRM Destination Component of KingswaySoft’s SSIS Integration Toolkit as part of November 2020 Release.

Check other posts on SSIS and Dynamics 365

https://nishantrana.me/2020/10/16/ssis-kingswaysoft-and-dynamics-365/

These options if enabled, will turn off or disable plugin, workflow, entity auditing during pre-execution temporarily, during the writing process, and will revert the changes to the original state after successful execution.

  • Tentatively Disable Relevant Plugins (if any)
  • Tentatively Disable Relevant Workflows (if any)
  • Tentatively Disable Relevant Auditing (if any)

Let us take a simple example to see it in action.

Here we have a simple data flow that creates contact records inside CDS using Data Spawner.

https://nishantrana.me/2020/05/26/using-data-spawner-component-ssis-to-generate-sample-data-in-dynamics-365/

Let us run the package and see the status of the plugin, workflow, and audit with regards to the contact entity.

As expected, we do not see any Audit records created for contact records creation.

We see an event where Audit is disabled and then enabled later for the contact entity, during the duration of package running and after its successful completion.

We can also see our workflow in status – Draft

In the case of Plugin, we didn’t see the step being disabled and it was still being triggered

As per the documentation –

Get the toolkit here

https://www.kingswaysoft.com/products/ssis-integration-toolkit-for-microsoft-dynamics-365

Hope it helps..

Changes in CDS / CRM Destination Component – SSIS Integration Toolkit for Dynamics 365


With version 20.2 November 2020 release, there have few updates added to the KingwaySoft’s CDS/CRM Destination Component, few are changes in the label and others are metadata changes, that one should be aware of before updating.

Check Post on SSIS and Dynamics 365

https://nishantrana.me/2020/10/16/ssis-kingswaysoft-and-dynamics-365/

  • Label changes.

Version 20.1 –

Version 20.2 –

  • CrmRecordId renamed to SavedRecordId

Version 20.1 –

Version 20.2 –

  • CrmErrorMessage renamed to ErrorMessage

Version 20.1 –

Version 20.2 –

Thus, any package saved with the new version will not work with the older version, and it is recommended to take full back up of the package before updating to the new version.

Hope it helps..

Sample code to connect to CDS / Dynamics 365 / CE using OAuth


Sample code for quick reference for connecting to CDS through a console application using OAuth

Add the NuGet package for Microsoft.CrmSdk.XrmTooling.CoreAssembly in the project.

Xrm.Tooling is the preferred way to connect to CDS, because of many benefits – we can define connection string, thread safety, support for X.509 certificate authentication, support for secure storage of sign-in credentials and reuse etc.

Note

Here we will be using the sample AppId and Redirect URI.

Sample Code- 


using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Tooling.Connector;
using System;

namespace SampleConsoleApp
{
class Program
{
static void Main(string[] args)
{
string ConnectionString = "AuthType = OAuth; " +
"Username = [username]@[domain].onmicrosoft.com;" +
"Password = [password]; " +
"Url = https://[orgname].crm.dynamics.com;" +
"AppId=51f81489-12ee-4a9e-aaae-a2591f45987d;" +
"RedirectUri=app://58145B91-0C36-4500-8554-080854F2AC97;" +
"LoginPrompt=Auto";

CrmServiceClient svc = new CrmServiceClient(ConnectionString);

if (svc.IsReady)
{
var myContact = new Entity("contact");
myContact.Attributes["lastname"] = "Test";
myContact.Attributes["firstname"] = "User1";
svc.Create(myContact);
}
}
}
}


For .NET Framework 4.5.2 – we need to explicitly specify Tls1.2 as the default protocol. 

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

https://nishantrana.me/2018/01/

.NET Framework 4.6.2 uses TLS 1.2 as the default protocol.

 

More details on – Connection string parameters.

Hope it helps..

An error occurred when processing the security tokens in the message:You are using Ws-Tust authentication which has been deprecated and no longer supported in your environment. Please use oAuth2.0 authentication in Dynamics 365 / CDS


While trying to connect to Dynamics 365 instance through Online Federation authentication type (deprecated) using CDS/ CRM connection manager of KingswaySoft

For Console App – https://nishantrana.me/2020/11/09/sample-code-to-connect-to-cds-dynamics-365-ce-using-oauth/

you would get the below error –

KingswaySoft.IntegrationToolkit.DynamicsCrm.CrmServiceException: CRM service call returned an error: An error occurred when processing the security tokens in the message:You are using Ws-Tust authentication which has been deprecated and no longer supported in your environment. Please use oAuth2.0 authentication. (SSIS Integration Toolkit for Microsoft Dynamics 365, v20.2.0.3083 – devenv, v15.9.28307.905)System.ServiceModel.FaultException

: An error occurred when processing the security tokens in the message:You are using Ws-Tust authentication which has been deprecated and no longer supported in your environment. Please use oAuth2.0 authentication.

The solution is to use the OAuth connection type as shown below.

More on OAuth 2.0 – https://nishantrana.me/2019/08/30/oauth-2-0-with-dynamics-365-ce-web-api/

Here we have selected Password as OAuth type.

And specified username and password, and for Client App Id we have specified the default client app id.

2ad88395-b77d-4561-9441-d0e40824f9bc

https://nishantrana.me/2019/08/23/connect-to-dynamics-365-web-api-using-oauth-2-0-resource-owner-password-credential-ropc/

This connection succeeds this time.

The same will be the experience with XrmToolBox while trying to create a new connection using the connection wizard

Microsoft.Xrm.Tooling.Connector.CrmServiceClient    Error    2    06/11/20 07:18:46 Unable to connect to CRM: An error occurred when processing the security tokens in the message:You are using Ws-Tust authentication which has been deprecated and no longer supported in your environment. Please use oAuth2.0 authentication.

Error    : An error occurred when processing the security tokens in the message:You are using Ws-Tust authentication which has been deprecated and no longer supported in your environment. Please use oAuth2.0 authentication.

Try the alternate connection method like SDK Login Control, Client ID secret, etc.

https://www.xrmtoolbox.com/documentation/for-users/connecting-to-an-organization/

Please refer to the below articles to get more details –

Use of Microsoft 365 authentication with the WS-Trust security protocol

Deprecation of Office365 authentication type and OrganizationServiceProxy class for connecting to Common Data Service

https://colinvermander.wordpress.com/2020/02/09/cds-crm-sdk-ws-trust-auth-and-organizationserviceproxy-deprecated-what-does-it-mean/

Hope it helps..

Upcoming updates to Export to Data Lake Service – 2020 Release Wave 2


As we know – Export to Data Lake service enables continues replication of CDS entity data to Azure Data Lake Gen 2

Below are some the interesting update coming to it –

Configurable Snapshot Interval – currently the snapshots are created hourly– which will be configurable.

Cross Tenant Support – Currently Azure Data Lake Gen2 and CDS environment needs to be in same tenant. With the new update we will be able to use Azure Storage part of a different tenant.

Dashboard support that will show the count and visual trend of records especially helpful for large data sets.

Parquet format support – currently the CDS entity data is exported in .csv format, going forward with 2020 Release wave 2, the support for Parquet format will be added for improved efficiency terms of storage and performance.

Support for exporting Audit data that can be used for analysis and reporting.

Support for entities with attachments

Soft Delete – Deleting data from source but still keeping it in the Data Lake.

Get all details here –

https://docs.microsoft.com/en-us/power-platform-release-plan/2020wave2/cdm-data-integration/export-data-lake

Hope it helps..