{"id":22095,"date":"2018-04-21T18:39:00","date_gmt":"2018-04-21T18:39:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/premier_developer\/?p=22095"},"modified":"2019-03-01T09:03:16","modified_gmt":"2019-03-01T16:03:16","slug":"using-vsts-api-with-powershell-to-scaffold-team-projects","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/premier-developer\/using-vsts-api-with-powershell-to-scaffold-team-projects\/","title":{"rendered":"Using VSTS API with PowerShell to scaffold Team Projects"},"content":{"rendered":"<p>In this post, Senior Application Development Managers <a href=\"https:\/\/www.linkedin.com\/in\/arthurgarcia\/\">Art Garcia<\/a> demonstrates how to navigate the VSTS REST APIs.<\/p>\n<hr \/>\n<p>Visual Studio Team Services or VSTS has matured into one of the leading Application Lifecycle management tools available. It allows you to not only manage your work and team velocity, but it also is a great tool to use in building and deployment as well. If you have several projects both current and future, the setup and administration can sometimes be a challenge. You want to make sure all projects have certain team\u2019s setup and some basic build and release artifacts. This will make transitioning from one project to another seamless and make for a consistent experience.<\/p>\n<p>So therein lies the problem. I have multiple project I need to create and assign users and teams to. Fortunately, VSTS has a rich array of REST API\u2019s to help. Everything from creating a project to adding work items and almost everything in between. For this discussion we will cover adding a project, adding teams to the project and finally adding users to the teams. We will also cover adding account level groups as well. So, let\u2019s get started.<\/p>\n<p>The place you will spend most of your time will be the <a href=\"https:\/\/www.visualstudio.com\/en-us\/docs\/integrate\/api\/overview\">VSTS REST API<\/a> documentation. Here is where you will find the REST calls to manage VSTS. We will start with the Project and Teams.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image00214.jpg\"><img decoding=\"async\" title=\"clip_image002\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image002_thumb13.jpg\" alt=\"clip_image002\" width=\"1028\" height=\"722\" border=\"0\" \/><\/a><\/p>\n<p>Here you navigate to the Create a team project. As with many of the REST calls in VSTS, it\u2019s not just one call to get what you are after. For the create project you will run the following POST operation.<\/p>\n<p><span style=\"font-family: Consolas\">POST https:\/\/{instance}\/defaultcollection\/_apis\/projects?api-version={version}<\/span><\/p>\n<p>The {instance} is your VSTS account i.e. <strong>myaccount.visualstudio.com<\/strong>, the version is the latest version of the API which is found in the documentation for the API. You will need to add the request for this operation and here is where it gets interesting. The request for this is as follows:\n<img decoding=\"async\" class=\"alignnone size-full wp-image-35591\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/art1.png\" alt=\"\" width=\"644\" height=\"182\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/art1.png 644w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/art1-300x85.png 300w\" sizes=\"(max-width: 644px) 100vw, 644px\" \/>\nThe first few, name, description and what source control type are straight forward. The process template is the one that will take another call. You need the GUID of the process you want to use. Agile, Scrum, CMMI, or any custom process in your account has a unique identifier. We need that GUID here in the request. Fortunately, that\u2019s an easy call to the API.<\/p>\n<p><span style=\"font-family: Consolas\">GET <a href=\"https:\/\/{instance}\/DefaultCollection\/_apis\/process\/processes?api-version={version\">https:\/\/{instance}\/DefaultCollection\/_apis\/process\/processes?api-version={version<\/a>}<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/image264.png\"><img decoding=\"async\" title=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/image_thumb244.png\" alt=\"image\" width=\"1028\" height=\"298\" border=\"0\" \/><\/a><\/p>\n<p>This will give you a list of processes. You can filter them by name to find the process you are looking for. Here is some PowerShell to find the process.<\/p>\n<p>&nbsp;<\/p>\n<p><code>$projectUri = \"<a href=\"https:\/\/&quot;\">https:\/\/\"<\/a> + $VSTSMasterAcct + \".visualstudio.com\/DefaultCollection\/_apis\/process\/processes?api-version=1.0\"<\/code><\/p>\n<p><code>$returnValue = Invoke-RestMethod -Uri $projectUri -Method Get -ContentType \"application\/json\" -Headers $authorization<\/code><\/p>\n<p><code>$id = ($returnValue.value).Where( {$_.name -match \u201cAgile\u201d })<\/code><\/p>\n<p><code>return $id.id<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Now you just replace the process id in the request and you are ready to create your project. Below is a snapshot of my CreateVSTSProject PowerShell function. If you notice on line 17, I call GetVSTSProcesses and pass in my $userParams. This is a small JSON text that contains the project name, and a few more parameters I need for my code to run.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image0049.jpg\"><img decoding=\"async\" title=\"clip_image004\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image004_thumb9.jpg\" alt=\"clip_image004\" width=\"1028\" height=\"614\" border=\"0\" \/><\/a><\/p>\n<p>The parameter file looks like this. I reference this file throughout my scripts, so I can change things easily in one place.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image0069.jpg\"><img decoding=\"async\" title=\"clip_image006\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image006_thumb8.jpg\" alt=\"clip_image006\" width=\"1028\" height=\"261\" border=\"0\" \/><\/a><\/p>\n<p>I also check if the project exists, if it does the API will throw an error. I capture that error and return that the project exists.<\/p>\n<p>Ok, so now we have a valid Project in VSTS, now let\u2019s add some teams and Groups to the project. To do this we will make a call to an API in the same Project and Team family. The Create a Team REST call is as follows and the documentation can be found <a href=\"https:\/\/www.visualstudio.com\/en-us\/docs\/integrate\/api\/tfs\/teams#create-a-team\">here<\/a>.<\/p>\n<p><span style=\"font-family: Consolas\">POST https:\/\/{instance}.VisualStudio.com\/DefaultCollection\/_apis\/projects\/{project}\/teams?api-version={version}<\/span><\/p>\n<p>The {instance} is your vsts account i.e.. myaccount.visualstudio.com, the version is the latest version of the API which is found in the documentation for the API. The Project is the name of the project you want to add these teams to. The request for this is simple, just a name and description as shown below.\n<img decoding=\"async\" class=\"alignnone size-full wp-image-35592\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/art2.png\" alt=\"\" width=\"244\" height=\"80\" \/>\nHere is what the code looks like:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image0087.jpg\"><img decoding=\"async\" title=\"clip_image008\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image008_thumb7.jpg\" alt=\"clip_image008\" width=\"1028\" height=\"329\" border=\"0\" \/><\/a><\/p>\n<p>This will give you teams in the project you just created. These are teams only visible in the project you created. If you want to have a team span multiple projects, then you would create a group at the account level. This will require using the <a href=\"https:\/\/www.visualstudio.com\/en-us\/docs\/integrate\/api\/graph\/overview\">Graph<\/a> REST API. A word of caution here. As of this writing this API is in an Alpha preview. Really that just means it\u2019s not completely baked yet. They are still working out the usage patterns and hardening the code. That said, I still encourage you to use them, just remember if you find an issue, report it. That way we all benefit.<\/p>\n<p>So to add groups with the Graph API at the account level you will run the following REST call.<\/p>\n<p><span style=\"font-family: Consolas\">POST https:\/\/{instance}\/_apis\/graph\/groups?api-version={version}<\/span><\/p>\n<p>The request for this call is very straight forward, just the displayName and the description as seen below.\n<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/image266.png\"><img decoding=\"async\" title=\"image\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/image_thumb246.png\" alt=\"image\" width=\"644\" height=\"99\" border=\"0\" \/><\/a>\nThis call will either create a new account level group and return the group information or just return the group information from an existing group. Either way you get the group information. The code to add a VSST account level group is shown below<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image0105.jpg\"><img decoding=\"async\" title=\"clip_image010\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image010_thumb5.jpg\" alt=\"clip_image010\" width=\"1028\" height=\"137\" border=\"0\" \/><\/a><\/p>\n<p>So at the beginning of this blog I said I would create a project, add teams and account level groups. The only part missing is adding users to those groups and teams. This is actually very easy. All that\u2019s required is a simple Graph REST call. Again, as before, be forewarned, this API is in Alpha preview.<\/p>\n<p>So the code to add users to a group or team is as follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image0124.jpg\"><img decoding=\"async\" title=\"clip_image012\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/clip_image012_thumb4.jpg\" alt=\"clip_image012\" width=\"1028\" height=\"99\" border=\"0\" \/><\/a><\/p>\n<p>If you look closely, you see that this call is asking for the Group descriptor. So, remember when we discussed adding an account group? I mentioned that it would return the group information. One important part of that return is the group descriptor. You will need this to find the appropriate group to add users to. Once you have that it\u2019s a matter of adding the user\u2019s email in the request as shown in the code example.<\/p>\n<p>So that\u2019s a lot of information to digest. Believe me it took me a bit to wrap my head around the API and all its capable of. These API\u2019s are a wonderful way to automate and standardize many VSTS functions and manual tasks. What I have shown is just the beginning. Now we have the project, the teams and the users in the teams or groups. What about a build script or a release definition or maybe adding some standard work items? All are possible with the API.<\/p>\n<p>I trust this has given you a small glimpse into what is possible with the API. The next entry in the series will be adding a build and release to the project you just created. Then we will tackle the security. Securing the teams and groups to allow or deny what operations can be performed. That\u2019s where it gets interesting. In my upcoming post, I will demonstrate the VSTS REST APIs to secure Team Projects.<\/p>\n<p>Get the source from this article on GitHub <a href=\"https:\/\/github.com\/artgarcia\/VSTSPowerShell\">here<\/a>.<\/p>\n<hr \/>\n<p><a href=\"https:\/\/blogs.msdn.com\/b\/premier_developer\/archive\/2014\/09\/15\/welcome.aspx\"><strong>Premier Support for Developers<\/strong><\/a> provides strategic technology guidance, critical support coverage, and a range of essential services to help teams optimize development lifecycles and improve software quality.\u00a0 Contact your Application Development Manager (ADM) or <a href=\"https:\/\/blogs.msdn.microsoft.com\/premier_developer\/contact-us\/\"><b>email us<\/b><\/a> to learn more about what we can do for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, Senior Application Development Managers Art Garcia demonstrates how to navigate the VSTS REST APIs. Visual Studio Team Services or VSTS has matured into one of the leading Application Lifecycle management tools available. It allows you to not only manage your work and team velocity, but it also is a great tool to [&hellip;]<\/p>\n","protected":false},"author":582,"featured_media":37840,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[35],"tags":[324,3,38],"class_list":["post-22095","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-alm","tag-rest","tag-team","tag-vsts"],"acf":[],"blog_post_summary":"<p>In this post, Senior Application Development Managers Art Garcia demonstrates how to navigate the VSTS REST APIs. Visual Studio Team Services or VSTS has matured into one of the leading Application Lifecycle management tools available. It allows you to not only manage your work and team velocity, but it also is a great tool to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/22095","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/users\/582"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/comments?post=22095"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/22095\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media\/37840"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media?parent=22095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/categories?post=22095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/tags?post=22095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}