Showing posts with label Dynamics 365. Show all posts
Showing posts with label Dynamics 365. Show all posts

Monday, 25 June 2018

Resolve Error "Unable to Login to Dynamics CRM" after v9 update




Recently we have updated our Dynamics 365 to v9 and we are updating our applications to the latest version of SDK.

As per Microsoft Guideline, approach is to update apps to .NET Framework 4.6.2 and update CRM SDK to latest v9.0 assemblies. But application was still throwing same error message ("Unable to Login to Dynamics CRM").

One of my colleague pointed to a David Branscome's Post about moving away from TLS 1.0 and 1.1 to 1.2 and it reminded me about the post I read few days back.

Because our .NET assemblies were already updated so version 4.6.2 and it should be handling the TLS Protocol issue itself. But our application was still throwing the same error so we decided to try invoke TLS1.2 manually in our application before CrmServiceClient is instantiated. Following line was added to the code:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

But it was still throwing same error and then we started looking at assembly references and app.config. In config we found that targetFramework was pointing to 4.5.2 and it worked when we changed to 4.6.2 (as shown below). Once it is updated connection will was created without any issue.


<httpRuntime targetFramework="4.6.2" />



Premjit Singh
Attendite Ltd.
Twitter

Friday, 28 April 2017

How to Retrieve record(s) from Dynamics CRM using FetchXml and Web Api

Following JavaScript shows how to retrieve record(s) from Dynamics CRM using FetchXml and Web Api.

function RetrieveUsingFetch() {
var date = new Date();
var today = date.DateFormat("yyyy-MM-dd"); \\ Click here to see function details.
var marketCondition = "";
var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
"<entity name='contractdetail'>" +
"<attribute name='contractid' />" +
"<attribute name='title' />" +
"<attribute name='contractdetailid' />" +
"<filter type='and'>" +
"<condition attribute='statuscode' operator='in'>" +
"<value>2</value>" +
"<value>1</value>" +
"</condition>" +
"<condition attribute='activeon' operator='on-or-before' value='" + today + "' />" +
"<condition attribute='expireson' operator='on-or-after' value='" + today + "' />" +
"<condition attribute='customerid' operator='eq' value='" + Xrm.Page.getAttribute("dbc_customerid").getValue()[0].id + "' />" +
"</filter>" +
"<link-entity name='contract' from='contractid' to='contractid' alias='aa'>" +
" <attribute name='title' />" +
"<filter type='and'>" +
"<condition attribute='statuscode' operator='eq' value='3' />" +
"</filter>" +
"</link-entity>" +
"</entity>" +
"</fetch>";

var uri = "/contractdetails?fetchXml=" + encodeURIComponent(fetchXml);
var clientUrl = Xrm.Page.context.getClientUrl();
var webApiPath = "/api/data/v8.1";
uri = clientUrl + webApiPath + uri;

var request = new XMLHttpRequest();
request.open("GET", encodeURI(uri), false);
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
request.setRequestHeader("OData-MaxVersion", "4.0");
request.setRequestHeader("OData-Version", "4.0");
request.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
request.setRequestHeader("Prefer", "odata.maxpagesize=10");
request.onreadystatechange = function () {
if (request.readyState === 4 /* complete */) {

if (request.status === 200) {
request.onreadystatechange = null; 
var data = JSON.parse(request.response);
if (data.value.length === 1 || data.value.length > 1) {
alert(data.value[0].contractdetailid);
}
} else {
var error = JSON.parse(request.response).error;
alert(error.message);
}
}
};
request.send();
}

Note: Make sure that you have right version of WebApi in your code.

Happy Coding

P. S. Hayer
(ਪ੍ਰੇਮਜੀਤ ਸਿੰਘ ਹੇਰ)

Please check my other (non-CRM) blog here: Programming Blogs