Calling Dataverse Web API in PowerShell using Client Credentials
Connecting to Dataverse using PowerShell can be very helpful for data migrations and use within Azure DevOps. Connecting to an instance in a non-interactive way can be tricky though. This article will provide you the links you need for creation and App registration and adding the app user to your environment. You can then utilize the script provided to call Web API requests including ones you define using the new Custom API functionality now available.
The script was written so that it is not dependent on any outside libraries such as the Microsoft.Xrm.Tooling connector. This is helpful in situation where involving an outside library will slow down your deployment time by having to be approved in a change control board.
If utilizing an outside code library is not a concern you can create a connection to Dataverse utilizing the Microsoft.Xrm.Tooling connector Get-CrmConnection cmdlet.
Create App Registration
The first thing to do is create an App Registration within Azure AD for Dataverse. To do this follow the instructions Microsoft has provided.
Add App User to Dataverse
Next add an Application User to Dataverse. To do this follow the instructions Microsoft has provided. Application users additional are also now available within the Power Platform Admin Portal
PowerShell Script
Utilize the script below to connect get the Authorization token and make any Web API calls you want. The Web API call in this script just pulls back the fullname of the first 10 contacts in the system. If you want to view this file in GitHub click here.
<#
.SYNOPSIS
Connect to Dataverse and run Custom API Function
.NOTES
Author : Richard Wilson
.PARAMETER $oAuthTokenEndpoint
The v2 OAuth endpoint for the App registration. This can be found by opening the App registation and
clicking the Endpoints button in the Overview area. Copy the OAuth 2.0 token endpoint (v2) url.
.PARAMETER $appId
The Application (client) ID of the App registration
.PARAMETER $clientSecret
The client secret generated within the App registration
.PARAMETER $dataverseEnvUrl
The url of the Dataverse environment you want to connect to
#>
param
(
[string] $oAuthTokenEndpoint = 'https://login.microsoftonline.com/{YourTenantId}/oauth2/v2.0/token',
[string] $appId = 'xxxxxxxxxx',
[string] $clientSecret = 'xxxxxxxxxxx',
[string] $dataverseEnvUrl = 'https://{YourEnvironmentId}.crm.dynamics.com'
)
##########################################################
# Access Token Request
##########################################################
# OAuth Body Access Token Request
$authBody =
@{
client_id = $appId;
client_secret = $clientSecret;
# The v2 endpoint for OAuth uses scope instead of resource
scope = "$($dataverseEnvUrl)/.default"
grant_type = 'client_credentials'
}
# Parameters for OAuth Access Token Request
$authParams =
@{
URI = $oAuthTokenEndpoint
Method = 'POST'
ContentType = 'application/x-www-form-urlencoded'
Body = $authBody
}
# Get Access Token
$authRequest = Invoke-RestMethod @authParams -ErrorAction Stop
$authResponse = $authRequest
##########################################################
# Call Dataverse WebAPI using Authentication Token
##########################################################
# Params related to the Dataverse WebAPI call you will be making.
# These need to be in single quotes to ensure they are not expanded.
$uriParams = '$top=5&$select=fullname'
# Parameters for the Dataverse WebAPI call which includes our header
# that carries the access token.
$apiCallParams =
@{
URI = "$($dataverseEnvUrl)/api/data/v9.1/contacts?$($uriParams)"
Headers = @{
"Authorization" = "$($authResponse.token_type) $($authResponse.access_token)"
}
Method = 'GET'
}
# Call the Dataverse WebAPI
$apiCallRequest = Invoke-RestMethod @apiCallParams -ErrorAction Stop
$apiCallResponse = $apiCallRequest
#Output the results
Write-Host $apiCallResponse.value
Thanks for sharing the best information and suggestions, it is very nice and very useful to us. I appreciate the work that you have shared in this post. Keep sharing these types of articles here.
ReplyDeleteGet Best Cfd Trading Platform
All the information you mentioned in your content it's very useful for them who have searched for this. They find best results with this. Thanks for sharing Your valuable information with us. Who Called Me in UK
ReplyDeleteThank you for sharing such a useful article. I had a great time. This article was fantastic to read. Continue to publish more articles on
ReplyDeleteData Engineering Services
Data Analytics Solutions
Data Modernization Solutions
AI & ML Service Provider
Share great information about your blog , Blog really helpful for us . Best Api Trading Platform
ReplyDeleteA very delightful article that you have shared here.Devops Support Company Your blog is a valuable and engaging article for us, and also I will share it with my companions who need this info. Thankful to you for sharing an article like this.
ReplyDeleteHello, i followed your precious guide using Postman form the client side. I correctly receive the token, but when I submit the REST GET request, I receive 401 unauthorized error. I performed all the steps:
ReplyDelete- Azure app registration
- Change of the values in the manifest
- Added the CRM Dynamics delegated permission
- Added the user application in Dataverse with administration and read security role
Where the problem could be?
Thanks!!!
same issues!
Delete
ReplyDeleteAmazing blog! I really like the way you explained such information about this post with us. And blog is really helpful for us this website
office-tool-plus-crack
flvto-license-key-crack-download
zookaware-pro-with-crack-latest/
adobe-dreamweaver-crack-key
smartdraw-crack-keys-download
ummy-video-downloader-crack
With your dedication and hard effort, you accomplished an outstanding job. Thank you for taking the time to share it with me. I really appreciate it.
ReplyDeleteclean-my-pc
I like your all post. You have done really good work. Thank you for the information you provide, it helped me a lot. I hope to have many more entries or so from you.
ReplyDeleteVery interesting blog.
Gravit Designer Pro Crack
Ummy Video Downloader Crack
MacDrive Pro Crack
WebcamMax Crack
Panda Dome Premium Crack
Camtasia Studio Crack
NTLite Crack
iMazing Crack
downloadpc.co
ReplyDeleteI am a professional web blogger so visit my website link is given below!
GraphPad Prism crack
Sony Vegas Pro Crack
Avast Driver Updater
Epubor All DRM Removal Crack
https://freeprosoftz.info/
had no idea of solving some basic issues. I do not know how to Crack Softwares Free Download But thankfully, I recently visited a website named ProCrackHere
ReplyDeleteEpubor All Drm Removal Crack
Reaper Crack
great post with nice content and thanks for sharing!!
ReplyDeletehow RPA solves
robotic process automation technology
Thanks For Your Blog. Are you looking for a Powerapps Developer With the Powerapps Developer Plan, you can develop your Power Applications and Power Automate skills while also receiving a free environment to build, test, and distribute applications.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteI'm experiencing issues on https://capcuttemplates.co/. It's currently facing performance and loading problems. Please check and resolve the issues.
ReplyDeleteThanks for sharing such valid informative post. In this post clearly explains the process. Keep sharing the post like this.
ReplyDeleteLow code development services
Mendix Services