Forum Discussion
dataverse for teams & vscode how to link??
I need to develop a teams app using teams toolkit in vscode.
At this time, the database should use dataverse for teams,
but I don't know how to connect it. Is there a way??
please hepl me...
- LeonPavesicSilver Contributor
Hi bingqiling,
According to the article "Creating a native Teams app with Dataverse integration" by Jan de Vries, to connect to the Microsoft Dataverse REST API in a native Teams app, you need to:
- Get the Web API endpoint for your environment. You can do this by navigating to the environment in the browser and searching for the Developer resources in settings.
- Create a JWT bearer token with the user_impersonation scope for Dynamics 365. To do this, you can use the following code:
private string dynamicsResource = "https://[thePowerPlatformEnvironment].api.crm4.dynamics.com/"; private string dynamicsScope = "user_impersonation"; private async Task ConsentAndShow() { try { await teamsUserCredential.LoginAsync(new[] { _scope, dynamicsResource + dynamicsScope }); NeedConsent = false; await ShowProfile(); await ShowDynamicsUserId(); } catch (ExceptionWithCode e) { ErrorMessage = e.Message; } }
- Add the token to the Authorization header of your requests to the Dataverse REST API.
For example, to query the accounts in Dataverse, you could use the following code:
private async Task LoadDataverseData() { var dynamicsToken = (await teamsUserCredential.GetTokenAsync(new TokenRequestContext(new string[] { dynamicsResource + dynamicsScope}), new System.Threading.CancellationToken())).Token; var httpClient = HttpClientFactory.CreateClient(); httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", dynamicsToken); var accountsResponse = await httpClient.GetAsync(dynamicsResource + "/api/data/v9.2/accounts?$select=name,revenue&$orderby=revenue desc&$top=3"); if (accountsResponse.IsSuccessStatusCode) { var topAccountsWithRevenue = await JsonSerializer.DeserializeAsync<Models.Dataverse.Accounts.WithRevenue.Rootobject>( await accountsResponse.Content.ReadAsStreamAsync()); _accountCollection.Clear(); _accountCollection.AddRange(topAccountsWithRevenue.value); } else { ErrorMessage += "Web API call failed"; ErrorMessage += "Reason: " + accountsResponse.ReasonPhrase; } }
For more information, please see the article "Creating a native Teams app with Dataverse integration" by Jan de Vries.
https://jan-v.nl/post/2022/creating-a-native-teams-app-with-dataverse-integration/Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
(LinkedIn)