UrbanPro
true

Find the best tutors and institutes for Microsoft Azure Training

Find Best Microsoft Azure Training

Please select a Category.

Please select a Locality.

No matching category found.

No matching Locality found.

Outside India?

Search for topics

OAuth Authentication (without using ADAL) to Dynamics 365 using Azure Apps

Jayakar Reddy
13/06/2018 0 0

Here I am going to show without using ADAL(active directory authentication library) how to get the authentication token and how to connect to CRM from a standalone HTML Page using the web-API.

I will not explain in detail about how to register an APP in azure and give it access to Dynamics CRM.

Use below code to get the OAuth token once you’re done with Azure app registration

var microsoftTokenUrl = "https://login.microsoftonline.com/4060bfe9-7199-xxxxxxx-xxxxxxx/oauth2/token"; //Add your endpoint URL
var clientId = "xxxxxxxx-9715-xxxx-xxxx-3a1fac0cc5fe"; // Add your app ID
var clientSecret = "xxxxxxMJTQmtu4V73cRyduZ6vS40AlkAtSxxxxxxx";//Add your Secret Key
var resource = "https://xxxxxxx.crm5.dynamics.com";//Add your CRM Url
var grantType = "client_credentials";
function GetAuthroisationToken() {
var token=null;
$.ajax({
url: microsoftTokenUrl,
type: "POST",
contentType: "application/x-www-form-urlencoded",
crossDomain: true,
dataType: "json",
async: false,
data: {
'resource': resource,
'client_id': clientId,
'client_secret': clientSecret,
'grant_type': grantType
},
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, xhr) {
token= data.access_token;
},
error: function (xhr, textStatus, errorThrown) {
alert(textStatus);
}});
return token;
}

Here is the sample code to create the lead in CRM (Design your HTML as your wish)

var entity = {};
entity.subject = $("#subject").val();
entity.firstname = $("#fname").val();
entity.lastname = $("#lname").val();
var salutation = $("#title option:selected").val();
entity.address1_postalcode = $("#postalcode").val();
entity.address1_city = $("#city").val();
entity.address1_stateorprovince = $("#state").val();
entity.address1_country = $("#country").val();
entity.address1_line1 = $("#address").val();
entity.emailaddress1 = $("#email").val();
entity.telephone1 = $("#phone").val();
entity.companyname = $("#company").val();
entity.jobtitle = $("#function").val();
entity.leadsourcecode = 8;
var token = GetAuthroisationToken();
if(token!=null)
webApi_Create("leads", entity, false,token);
function webApi_Create(entityName,entityObject,isAsync,token)
{
var newEntityId = null;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: resource + "/api/data/v8.2/" + entityName,
data: JSON.stringify(entityObject),
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
XMLHttpRequest.setRequestHeader("OData-Version", "4.0");
XMLHttpRequest.setRequestHeader("Accept", "application/json");
XMLHttpRequest.setRequestHeader("Authorization", "Bearer " + token);
},
async: isAsync,
success: function (data, textStatus, xhr) {
var uri = xhr.getResponseHeader("OData-EntityId");
var regExp = /\(([^)]+)\)/;
var matches = regExp.exec(uri);
newEntityId = matches[1];
if (newEntityId !== null)
alert("Record Created!");
},
error: function (xhr, textStatus, errorThrown) {
alert(textStatus + " " + errorThrown);
}
});
return newEntityId;
}

 
 
0 Dislike
Follow 2

Please Enter a comment

Submit

Other Lessons for You

Dialog Runbase Example in AX 2012
class Runbase_Example extends RunBase{TransDate fromDate,toDate;CustAccount custAccount;DialogField dlgFromDate,dlgToDate,custDldField;#define.CurrentVersion(1)#localmacro.CurrentListfromDate,toDate#endmacro}protected...
S

Shiva Kumar | 09/11/2020

0 0
0

Azure Storage Gen1 VS Azure Storage Gen1
We have azure storage from the beginning now Microsoft Azure introduce azure Storage Gen2, here we have BLOB+Data Lake Gen2 is combined But we should remember here for few features of data lake gen...
M

Mahesh Kumar | 30/06/2020

0 0
0

Difference between Union and Union All
Both Union and Union All are Used to Join Rows of two or more tables.But the Union Or Union All need the datatype same. Let's Understand with the below example. Difference-1 UnionUnion will combine...

Abinash Hota | 05/07/2019

0 0
0

What is M.S.Project ?
MICROSOFT PROJECT contains project work and project groups, schedules and finances.Microsoft Project permits its users to line realistic goals for project groups and customers by making schedules, distributing...

Azure Course Content
Microsoft Azure Course Content Lesson 1: Introduction to Azure Overview of On-premise infrastructure The transition from On-premise to datacenter housing & managed data centres and finally...

Shubham Garg | 13/07/2018

1 0
0

Looking for Microsoft Azure Training ?

Find best Microsoft Azure Training in your locality on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
Sponsored
X

Looking for Microsoft Azure Training Classes?

Find best tutors for Microsoft Azure Training Classes by posting a requirement.

  • Post a learning requirement
  • Get customized responses
  • Compare and select the best

Looking for Microsoft Azure Training Classes?

Find best Microsoft Azure Training Classes in your locality on UrbanPro

Post your learning requirement

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more