erotavlas
erotavlas

Reputation: 4475

Unable to find type [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]

I'm trying to write a script to connect to TFS using powershell, however I'm stuck on the part of actually connecting

$credentialProvider = new-object Microsoft.TeamFoundation.Client.UICredentialsProvider
    $collection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($uri, $credentialProvider)

It gives an error that says it cannot find the type

[ERROR] New-object : Cannot find type [ERROR] [Microsoft.TeamFoundation.Client.UICredentialsProvider]: verify that the [ERROR] assembly containing this type is loaded.

Well I tried to do this first, but it did not help

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.WorkItemTracking.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client")

I only have Visual Studio 2015 installed on my development environment. Is there some component that I'm missing that is a requirement for interfacing with TFS using powershell?

Furthermore I don't know where this script will be run from (it wont be from a development machine), presumably from a machine that has access to TFS server directly maybe using Team Explorer.

Upvotes: 3

Views: 5100

Answers (3)

jessehouwing
jessehouwing

Reputation: 114461

The Team Foundation Server Client Object Model used to be installed to the Global Assembly Cache when you installed Team Explorer 2013 or below. Because of that, they were always easy to load from any script.

With Team Explorer and Visual Studio 2015 and up, the packages are no longer registered globally. At the same time, Microsoft changed the license and made these assemblies distributable with your application and released a NuGet package to make distribution easier.

The proper way to handle scenarios where you need the TFS Client Object Model is to package them with your script or the download them on-demand using Nuget.

There are a number of packages that you may or may not need depending on what you are doing from your scripts:

Traditional Client Object Model:

New-style REST API object model:

You can use this little snippet to fetch nuget.exe and the dependencies on the fly: https://stackoverflow.com/a/26421187/736079 or use the install-package that was introduced in powershell v5.

Note: If you're updating or creating new scripts, it's recommended to switch to the new-style REST API's and the object model that goes along with that.

Upvotes: 4

Cece Dong - MSFT
Cece Dong - MSFT

Reputation: 31003

In Visual Studio 2015, the object model client libraries are removed from GAC. In order to load them you need to point the Add-Type cmdlet to a path, for example:

Add-Type -Path "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Microsoft.TeamFoundation.Client.dll"

Otherwise, you could install package from Nuget as @Jessehouwing mentioned.

Upvotes: 0

Thom Schumacher
Thom Schumacher

Reputation: 1583

Here is what I used to pull in the dll's for 2013,2015 tfs

function Connect-ToTfs
{
    Param([string] $Collectionurl)
    #the collection url will be cast as a uri to the getteamproject collection. 
    Write-Verbose $Collectionurl
    if ($CollectionUrl -ne '')
    {
        #if collection is passed then use it and select all projects
        $tfs = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection([uri]$CollectionUrl)
    }
    else
    {
        #if no collection specified, open project picker to select it via gui
        $picker = New-Object Microsoft.TeamFoundation.Client.TeamProjectPicker([Microsoft.TeamFoundation.Client.TeamProjectPickerMode]::NoProject, $false)
        $dialogResult = $picker.ShowDialog()
        if ($dialogResult -ne 'OK')
        {
            #exit
        }
        $tfs = $picker.SelectedTeamProjectCollection
    }
    $tfs    
}
function Invoke-VisualStudioDlls
{
    if (Test-Path 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer')
    {
        Write-Verbose "importing Visual Studio 2015 Dll's"
        Invoke-Visual15StudioDlls
    }
    elseif (Test-Path 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v2.0')
    {
        Write-Verbose "importing Visual Studio 2013 Dll's"
        Invoke-Visual13StudioDlls
    }
}
function Invoke-Visual15StudioDlls
{
    $visualStudiopath = 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer'
    #$visualStudiopath45 = 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer'
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.VersionControl.Client.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Common.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Client.dll"
    Add-type -path "$visualStudiopath\Microsoft.TeamFoundation.ProjectManagement.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Build.Common.dll"

}

function Invoke-Visual13StudioDlls
{
    $visualStudiopath = 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ReferenceAssemblies\v2.0'
    $visualStudiopath45 = 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\ide\ReferenceAssemblies\v4.5'
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.VersionControl.Client.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Common.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
    Add-Type -Path "$visualStudiopath\Microsoft.TeamFoundation.Client.dll"
    Add-type -path "$visualStudiopath45\Microsoft.TeamFoundation.ProjectManagement.dll"  
}

Upvotes: 1

Related Questions