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..

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..

Snapshot in Azure Data Lake (Dynamics 365 / CDS)


In the previous post, we saw how to export CDS data to Azure Data Lake Gen 2

https://nishantrana.me/2020/09/07/export-data-from-common-data-service-to-azure-data-lake-storage-gen2/

Here let us have a look how the sync and snapshot work.

We have already done the configuration and have synced the Account and Contact entity.

As the diagram depicts – there is initial sync followed by incremental writes for both entity data and metadata.

Below we can see we had initial sync completed for both contact and account entity, followed by an update in contact record which triggered another incremental sync – last synchronized on.

Within our storage container, we can see a folder created for each of the entities selected for sync.

As a part of initial sync or export, the entity data is written in CSV files

Changes in CDS are pushed to the CSV files continuously. (create, update and delete) along with a snapshot.

Within the Snapshot folder, we can see the snapshot added if there any change in the entity’s data.

Here for an account, we had no change.

Now let us look at the contact entity, here we can see the update in the 2020.csv file.

Let us look at the snapshot generated for contact; it shows no update as we have checked it within a 1-hour window of the update.

Snapshot is a read-only copy of data, which is updated at regular interval i.e. 1 hour, this ensures that the user is looking at a data which is not constantly getting refreshed.

Refreshing after some time, we can see the new snapshot added for the Contact entity.

As expected, no new snapshot for the Account entity as there was no change.

Let us create a new contact record, and see when we see the update in the CSV file and when does the new snapshot gets created.

We have created a new contact record around 8:30.

This was earlier before the update was made

Within 5 minutes we can see the update in the CSV file

And as expected another snapshot at 9:16 exactly after one hour of the first snapshot.

Get all the details here

https://docs.microsoft.com/en-us/powerapps/maker/common-data-service/export-to-data-lake

Hope it helps..