user3432571
user3432571

Reputation: 77

Get shelveset items programatically with C# from TFVC/TFS API

I implemented TFGet to download the latest items, given the TFS URL, TFS Path and the Local Path.

I need to implement code for unshelving the shelveset when the shelveset is selected from a TFVC/AzureDevOpsServer Build Definition.

I'm using the following code to get the item list for downloading the files and for controlling the folders to be created or deleted:

ItemSet = VcsRef.GetItems(
    TfsPath, 
    VersionSpec.Latest,
    RecursionType.Full,
    DeletedState.NonDeleted,
    ItemType.File,
    true);

I'm using the following code for downloading the shelveset details:

PendingSet[] pendingSets = VcsRef.QueryShelvedChanges("tfget", "userid", null, true);

if (pendingSets.Length <= 0)
{
    Console.Error.WriteLine("Unable to retrieve shelveset. Do you have permissions for retrieving this shelveset?");
    Environment.Exit(1);
}
else if (pendingSets.Length > 1)
{
    Console.Error.WriteLine("Multiple shelvesets match the query arguments.");
    Environment.Exit(1);
}

PendingChange[] pendingChanges = pendingSets[0].PendingChanges;
if (pendingChanges.Length <= 0)
{
    Console.Error.WriteLine("No pending changes could be found in this shelveset.");
    Environment.Exit(1);
}

foreach (PendingChange pendingChange in pendingChanges)
{
    Console.WriteLine("Processing pending change:", pendingChange.ToString());

    // Only edited files - Nothing to do when file doesn't exist in the shelveset
    if (pendingChange.ItemType != ItemType.File ||
        !pendingChange.IsEdit ||
        pendingChange.IsAdd ||
        pendingChange.IsBranch)
    {
        continue;
    }

    string relativePath = pendingChange.ToString().Remove(0, TfsPath.Length);
    string targetPath = Path.Combine(Local, relativePath.Replace("/", @"\"));

    // Download:
    pendingChange.DownloadShelvedFile(targetPath);
}

However, I'm unable to download the shelveset items... Is this the correct approach?

Upvotes: 1

Views: 632

Answers (1)

Cece Dong - MSFT
Cece Dong - MSFT

Reputation: 31075

You could try REST API to get a collection of shallow shelveset references:

Get http://TFS:8080/tfs/DefaultCollection/_apis/tfvc/shelvesets?requestData.includeLinks=true&api-version=3.2-preview

Upvotes: 0

Related Questions