Calling Dataverse Web API in PowerShell using Client Credentials

PowerShell Plus Dataverse

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
App User 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

Comments

  1. 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.
    Get Best Cfd Trading Platform

    ReplyDelete

  2. Wonderful Post!!! Thanks for sharing this great blog with us.
    Why DevOps?
    Why use DevOps?

    ReplyDelete
  3. 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

    ReplyDelete
  4. Thank you for sharing such a useful article. I had a great time. This article was fantastic to read. Continue to publish more articles on
    Data Engineering Services 
    Data Analytics Solutions
    Data Modernization Solutions
    AI & ML Service Provider

    ReplyDelete
  5. Share great information about your blog , Blog really helpful for us . Best Api Trading Platform

    ReplyDelete
  6. Your 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.

    ReplyDelete
  7. A 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.

    ReplyDelete
  8. Hello, 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:
    - 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!!!

    ReplyDelete
  9. 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.
    clean-my-pc

    ReplyDelete
  10. 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.
    Very 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

    ReplyDelete
  11. 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
    SmartDraw Crack
    Pinnacle Studio Crack

    ReplyDelete
  12. Wow, amazing block structure! How long
    Have 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

    ReplyDelete
  13. 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
    Epubor All Drm Removal Crack
    Reaper Crack

    ReplyDelete
  14. Thanks for this blog, keep sharing your thoughts like this...
    What is Microsoft Azure
    Microsoft Azure Benefits

    ReplyDelete



  15. After looking through a few blog articles on your website,
    we sincerely appreciate the way you blogged.
    We've added it to our list of bookmarked web pages and will be checking back in the near
    future. Please also visit my website and tell us what you think.
    coolmuster-android-backup-manager-crack
    sony-vegas-pro-crack
    ninjagram-crack
    techtool-pro-crack
    hot-alarm-clock-crack
    kurulus-osman-crack
    avast-premium-crack
    presonus-studio-one-pro-crack
    proteus-pro-crack

    ReplyDelete
  16. Great info. The content you wrote is very interesting to read. This will be loved by all age groups.
    DevOps Certification in Chennai
    DevOps Training Online
    DevOps Certification in Coimbatore

    ReplyDelete
  17. After looking through a few blog articles on your website,
    we sincerely appreciate the way you blogged. ecrack.org future. Please also visit my website and tell us what you think.

    Proteus Pro Crack

    ReplyDelete
  18. Thanks for this blog keep sharing your thoughts like this...
    HTML5 Training in Chennai
    HTML5 Course in Chennai


    ReplyDelete
  19. So nice I am enjoying for that post as for u latest version of this Security tool Available.
    UTorrent Pro Crack
    Color Finale Pro Crack
    ascrack.org

    ReplyDelete
  20. Very good article! We will be linking to this particularly great post on our website. Keep up the good writing.free4links.com
    Epubor DRM Removal Crack

    ReplyDelete
  21. I'm really impressed with your writing skills, as smart as the structure of your weblog.
    Ummy Video Downloader Crack
    up4crack.net

    ReplyDelete
  22. Very good article! We will be linking to this particularly great post on our website. Keep up the good writing.free4links.com
    DriverMax Pro Crack

    ReplyDelete
  23. Wow, amazing block structure! How long
    Have 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
    Apache Open Office Portable Crack
    Enscape 3D Crack
    SoundToys Crack
    Sonnox Oxford Reverb Crack

    ReplyDelete
  24. Very good article! We will be linking to this particularly great post on our website. Keep up the good writing.downloadpc.co
    SmartDraw Crack

    ReplyDelete
  25. Very good article! We will be linking to this particularly great post on our website. Keep up the good writing.free4links.com
    SmartDraw Crack

    ReplyDelete
  26. DriverDoc Product Key is an excellent program. You can also use an activation code obtained from the official website to activate it. In such cases, users receive complete support as well as the most recent software upgrades.
    Visit Here

    ReplyDelete

Post a Comment

Popular posts from this blog

Add User As Local Administrator On Domain Controller

Custom Grid/Subgrid Using Office-UI-Fabric DetailsList

Horizontally Scroll A Large Power Apps Gallery