AzureDevOps RESTAPIs Part-1 | .Net Core Project | Use APIs to fetch Repository and Pipeline data

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we will see how to use azure devops rest apis and fetch the repository and pipeline details from azure devops portal using rest apis so we will create a net core application where we will use a personal access token in azure devops and a new get package to fetch the repository and pipeline details and here are the prerequisites so you must have an azure account and basic understanding of azure devops portal and if you want to see the documentation so here is the link of the azure devops rest api documentation i'll share the link in the description of the video so let's create a dot net core application now before creating.net core application so here is the demo project ac in my azure devops portal and here are the repositories so we will fetch the details of these repositories uh through rest apis and similarly uh we will fetch the details of the uh pipelines uh so here i have two pipelines so we will fetch the name and link of the pipelines through rest apis so let's create a new.net core application in visual studio so i'll select an asp.net core uh web app let's name it azure devops api click next select dotnet 5.0 we will not configure it for https and click create now go to appsettings.json so we will create our two keys here we will specify the value later and second we will specify the key for a project name so our project name from azure devops is demo project ac so i'll copy this and paste it here so just save it now we will create a personal access token in azure devops now we will create a personal access token in azure devops so here i am logged into my azure devops portal so we will go to personal access token and click on new token we will name the token as azure rest token keep the other values as it is if you want you can select individual items uh for permissions here uh for now i will select full access and click on create click here to copy the token value we will use this value in our app settings.json now paste the token here that we have copied from azure devops portal and now we will add the nuget packages so the first package that we require is the microsoft team foundation server.client so this package will help us uh in providing classes and methods uh to send the request and fetch the response from azure devops rest apis so i'll go ahead and add this another package that we need uh is newtonsoft.json so add this also now after adding these two nuget packages now let's add a class uh which will have the methods to fetch uh the data from the azure rest apis so i'll create a new folder called services here and inside this folder let's add a new class uh so we will call this class as azure devops manager this is a plain c sharp class so first i'll define a constant string for storing our devops or url so our devops or url uh we can get from here so this would be our azure devops organization url so i'll copy this and add this here and then um now uh since we have defined the uh azure token and project name in our app settings file uh so we will need to fetch those details now from through the configuration object so i'll add a constructor here and in that constructor i'll pass the eye configuration so let's add the name space here using control dot so for this now i'll create a field also so i'll rename it as underscore config okay and now let's add couple of methods uh to fetch the token and project name from app setting so here i have added two methods uh one is get token config to get the azure token value defined in the app settings dot json similarly we have fetched the project name from the app settings dot json file now let's add a method that will authenticate our request to the azure devops apis and this will return a vss connection so to add this we will import this library so this is coming from the nuget package that we have added earlier called microsoft.teamfoundationserver.client so i'll add the using statement here now let's define a variable for token and another variable for project name now we will create a variable called credentials so this would be new vss basic credential for the first parameter we just have to pass string.mt and for the second parameter we will pass the token so for vss basic credential we have to add another using statement so i'll add this now let's create a connection variable so this will define our new vss connection so here we will pass our devops organization url so i'll create a new uri and i'll pass the credentials now we will return the connection object from here now let's create a new method that will return the list of git repositories from our azure devops portal so i'll define our new method that will return a list of git repository so this git repository class is also coming from the nuget package that we have added earlier let's name this method as get get our repose so first we will create an object so this will contain our uh authentication token and then we will define a get http client so this http client is again coming from the team foundation server or nuget package that we have added so this will be con dot get client and as a generic parameter it will take as get http client so here we will call the method to get all of our repositories get client dot get repositories async dot result we can also use await here but since we don't have uh that much repositories so i'll use dot result and we can just return all repositories object from here now we will create a dot cs html page to display the data we will use index.cshtml to display our data and the list of repositories that we get from azure devops for that we will call getgetrepost method on index.cshtml page so i'll first create a list of get repository here and add the using statement let's call it repositories so this list will hold all our repositories and we will use this as a model for our cs html page now to call the get git repose method i'll use dependency injection so for that let's extract the interface first hit ok so this will create a new interface inside the services folder it only has one method for now now uh we will have to add this dependency in our startup class so i'll go to startup and in configure services method i'll add services dot add transient i azure devops manager this is the interface that we have created and the implementation class now in index.cshtml.cs we can just inject this let's add the using statement let's call this devops manager and let's add a field for this also so i'll rename the field as underscore devops manager now in onget method let's populate our repositories object so repository is equals to underscore devops manager dot get get repose now in index.cs html i'll delete this default div and i'll add this div so here i have just added a div i've used the bootstrap css classes for container and row and i've created a table and inside that i have just created a header for table so if i run this now and place a debugger here so we can see that we are getting the three repositories that we have in our azure devops so here we are getting the name and the url of the repositories now let's display this data on our cs html page so for that below the header i'll use a for each loop and we can use our repositories model we will use model dot repositories and inside this let's create a row and for displaying our repository name let's create a column we can use item.name to display the column name and for displaying the url we will use anchor tag so in href we can call at item dot web url target equals to underscore blank and again use item dot web url to display the url now let's run this again hit f5 so here as you can see we have successfully fetched the repository name and repository url so if i go to this link it will redirect me to the azure devops portal if you are already signed in it will show the repository otherwise you have to sign in into your azure devops portal now to fetch the pipeline details let's create a model first so i'll add a folder here called models and inside model let's create a new class for pipeline model so i'll name it as pipeline entity so for first property i'll name it as string name and second property would be the url of our pipeline and in the interface let's add a new method which will return a list of pipeline entities let's add the using statement and name the method is get pipelines now let's implement the interface to add our newly added method for get pipelines so here first let's define a list of pipeline entity let's call it pipelines and i'll first define a variable for token format which is string dot format so for first value i'll pass is blank and for second value i'll pass the token using the getconfig method similarly and let's create a variable for credentials it will be convert dot to base64 string let's add the using statement for ascii encoding dot ascii get bytes and here let's add our variable token format now to call the pipelines using rest api in azure devops uh we don't have a built-in class like we have for like we have for git http client so we will use uh api slash pipelines uh method that azure devops provides so for that let's define our http client so client equals to new http client let's add the using statement for this so i'll first define the base address for client it will be new uri and here we will define our devops organization url plus the project name that we have defined in our config file and plus let's provide a forward slash here then we have to provide the authentication token in headers so for that first we will clear the default request headers so here i have cleared the accept header and i'll add uh default request headers dot accept dot add new system.net dot http and dot headers dot media type with quality header value here i'll define the accept header as application slash json and for authentication i'll define client dot default request headers dot authorization equals to news system dot net dot http headers dot authentication header value so here for the first parameter we will pass basic and for second parameter we will pass the credential variable now let's define the http response message and now we will call our azure devops rest api for getting the pipelines details so for that i will call client.get async and here i will define underscore api slash pipelines dot result so here again we can use await but since we don't have that much pipelines so i'm using dot result so apis slash pipelines is the rest api that is provided by azure devops that we can use to fetch the pipeline details so now we just have to check the response if it is successful then i'll define the json result to hold the result data so it would be response dot content dot read as a string async dot result for d serializing this json result object now we have to use expand object so i'll explain that now when we will debug this method so for now let's define a dynamic variable so in this d serialized variable we will use jsonconvert.dcrealize object method from newtons of json to deserialize our json result data so i'll use json convert let's add the using statement dot d serialize object here i'll define the expand object and for the parameter value i will define the json result and for serializer settings we will define the new expand object converter let's add the using statement for this the reason for using expand object is since in this json result we will get various values related to pipelines and we haven't defined those properties in our model object so in our model object we only have name and url so we cannot deserialize the json result directly into our model object so that is why i have used expand object it would be more clear when we debug this method so now we will look through the deserialized json object for each variable item in decentralization dot value so here we have to cast it into i numerable of type dynamic and then we can populate our entity object here so i'll create a new object of our pipeline entity call it entity equals to new pipeline so for name i'll populate it as item dot name and for url so in the json result we get the url under item dot underscore links dot web dot href now add this object in our pipelines list and at last we will return the pipelines now let's add a cs html page to display our pipeline data so i'll add a new razor page here let's add an empty page call it pipelines so here i'll add a div structure that i have already prepared so in this div i have a container and inside that container i have defined a table so we will display the pipeline name and the pipeline url that we are fetching from our get pipelines method so for that we will define this model.pipelines in our cshtml.cs page now so let's go to the code here before our onget method let's define the model public list of pipeline entity let's call it pipelines let's import the using statement for list and as we did for our index dot cs html page i'll define a constructor here and inject the i azure devops manager let's call it devops manager let's define a field also i'll rename it as underscore devops manager now inside onget method we can just populate our pipelines object pipelines equals to underscore devops manager dot get pipelines now to open our pipelines cshtml page i'll add a link in our layout master page so in layout.cshtml i'll replace the slash privacy with slash pipelines and let's add the text also as pipelines so this will show in our navigation bar now let's run this application so here we are getting a compilation error instead of item dot url we have to use capital url now let's run this application and before running the application i have put breakpoints on the get pipelines method now let's run the application now let's go to pipelines hit f5 let's check the success code okay it is false now let's check the error now i have debugged the application earlier so i know the issue is in base address so here after the devops org url we have to pass a forward slash now run the application again now let's hit f5 now this time we got the success code as true so now we are getting the data here so i will check the d serialized json object now here if we check the result view so this is the reason that we have to use the expand object because here we are getting these different properties from azure devops pipeline apis and we haven't created these properties in our model so to map the json result to our d serialized object we have to use expand object here now continue so here we are getting again an exception so let's check the error here we are getting the error in the name property so let's check the value in the item object so for name we have small n instead of capital n so i'll change that now let's run the application again go to pipelines hit f5 and now we are getting the pipelines data in the form of pipeline name and url so if i click the pipeline url it will redirect me to the pipelines page in the azure devops portal so we have successfully fetched the pipelines and repository details from azure devops using apis thank you for watching the video please like the video and subscribe to the channel
Info
Channel: Jitendra Panchal
Views: 11,976
Rating: undefined out of 5
Keywords: #Azure, #AzureDevOps, #AzureDevOpsRestApis, #RestApis, #Practical, #AzureProject, #AzureDevOpsProject, #Cloud, #MicrosoftAzure, #dotnet, #dotnetcore, #RealAzureProject
Id: xi3GYsbVgMI
Channel Id: undefined
Length: 29min 33sec (1773 seconds)
Published: Sun May 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.