Create a record with Typescript react app into Dataverse

Viewed 18

I am looking for a solution to create a record within Dataverse from a typescript app and all the information regarding authorisation etc.

I have found a nom package https://www.npmjs.com/package/dynamics-web-api

I need to know will this do the job and if so - any more information on the subject or has anyone done it ?

I am using but I am getting an error with this piece of code :

try{
    //call any function
    const responseDyn = async () => {
        await dynamicsWebApi.executeUnboundFunction('WhoAmI');
    }
    console.log(`Hello Dynamics 365! My id is: ${responseDyn.UserId}`);
}
catch (error){
    console.log(error);

it is saying UserId does not exist on type Promise(void) - how does it know because it hasn't got the json yet so to speak.

1 Answers

The error you're getting is a code error -- nothing to do with Dataverse. Try to wrap everything inside an async arrow function, or use then().

(async () => {
    const responseDyn = await dynamicsWebApi.executeUnboundFunction('WhoAmI');
    console.log(`Hello Dynamics 365! My id is: ${responseDyn.UserId}`);
})();

Next, it's perfectly possible to talk to Dataverse from a react app but the first challenge to overcome is authenticating to Azure & Dataverse and obtain a token to include in your web requests.

See here for an explanation about authentication being required, then here for all the detailed instructions & code samples create an Azure app then authenticate using MSAL.js (Microsoft Authentication Library).

The github library you are looking at is a 3rd-party library and not the official one. The github page explains that the library doesn't handle authentication for you. Nothing wrong with that library, just pointing it out since you might prefer using using standard fetch HTTP requests first. See Dataverse Web Api documentation.

Finally, no authentication is required to use the web api inside Dataverse model-driven app / canvas app -- since in those cases you're already logged in.

Related