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
ReplyDeleteWonderful Post!!! Thanks for sharing this great blog with us.
Why DevOps?
Why use DevOps?
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
ReplyDeleteYour blog article is outstanding; it is informative; in fact, I write on a variety of issues, including app development technology. Please read and give your thoughts on content related to mobile app development company for businesses.
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/
I guess I am the only one who came here to share my very own experience. Guess what!? I am using my laptop for almost the past 2 years, but I 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
ReplyDeleteSmartDraw Crack
Pinnacle Studio Crack
I thought this was a pretty interesting read when it comes to this topic. Thank you
ReplyDeleteAvira Antivirus Crack
VueScan Pro Crack
WebcamMax Crack
Allavsoft Video Downloader Converter Crack
up4crack.net
Wow, amazing block structure! How long
ReplyDeleteHave you written a blog before? Working on a blog seems easy.
The overview of your website is pretty good, not to mention what it does.
In the content!
vstkey.com
GraphPad Prism Crack
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
Thanks for this blog, keep sharing your thoughts like this...
ReplyDeleteWhat is Microsoft Azure
Microsoft Azure Benefits
Great info. The content you wrote is very interesting to read. This will be loved by all age groups.
ReplyDeleteDevOps Certification in Chennai
DevOps Training Online
DevOps Certification in Coimbatore
Very good article! We will be linking to this particularly great post on our website. Keep up the good writing.
ReplyDeleteDVDFab Crack
NTLite Crack
PC Cleaner Pro Crack
Driver Talent Pro Crack
SolidWorks Crack
Reimage PC Repair Crack
DriverDoc Crack
great post with nice content and thanks for sharing!!
ReplyDeletehow RPA solves
robotic process automation technology
Thanks for this blog keep sharing your thoughts like this...
ReplyDeleteHTML5 Training in Chennai
HTML5 Course in Chennai
출장샵
ReplyDelete출장샵
출장샵
출장샵
출장샵
출장샵
출장샵
Hi there! I just want to offer you a huge thumbs up for the great information you have here on this post. I’ll be coming back to your website for more soon.
I am a professional web blogger so visit my website link is given below! To get more information
ReplyDeletesoftwaretube.net/
Tipard Video Converter Crack/
I am a professional web blogger so visit my website link is given below! To get more information
ReplyDeletePanda Dome Premium crack
Amazing blog! I really like the way you explained such information about this post with us. And blog is really helpful for us this website.Devops online training in hyderabad
ReplyDeleteI am very interested in your articles,
ReplyDeleteI hope to read more of your articles in the future,
you can also visit the articles, I hope we can learn from each other
출장샵
아산출장샵
제주도출장샵
여수출장샵
김포출장샵
Book Illustration Service
ReplyDeleteThank you for sharing this information..
ReplyDeleteGraphPad Prism Crack Free Download