Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MirzaMerdovic/afed2324d83bf94dcdf9296f2909c4e1 to your computer and use it in GitHub Desktop.
Save MirzaMerdovic/afed2324d83bf94dcdf9296f2909c4e1 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.Common;
namespace BuildAndDeploy.GetSourceTest
{
class Program
{
static void Main(string[] args)
{
var path = "$/myproject/Main";
var personalAccessToken = "***************************";
var uri = new Uri("https://myproject.visualstudio.com");
var credentials = new VssBasicCredential("", personalAccessToken);
// You can use hard coded ProjectId or there is probably some better way to not always call this, but at the moment it's fine like this
Guid projectGuid = GetProjectId(uri, credentials);
using (var client = new TfvcHttpClient(uri, credentials))
{
var itemRequestData = Create(path);
Stream item = client.GetItemsBatchZipAsync(itemRequestData, projectGuid).Result;
SaveZippedContent(@"C:\Output\main.zip", item);
}
}
private static Guid GetProjectId(Uri uri, VssBasicCredential credentials)
{
using (var client = new ProjectHttpClient(uri, credentials))
{
return client.GetProject("MoveDesk").Result.Id;
}
}
private static TfvcItemRequestData Create(string folderPath)
{
return new TfvcItemRequestData
{
IncludeContentMetadata = true,
IncludeLinks = true,
ItemDescriptors =
new[]
{
new TfvcItemDescriptor
{
Path = folderPath,
RecursionLevel = VersionControlRecursionType.Full
}
}
};
}
private static void SaveZippedContent(string saveToPath, Stream content)
{
var buffer = new byte[1024];
try
{
using (var output = new FileStream(saveToPath, FileMode.Create))
{
int readBytes;
while ((readBytes = content.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, readBytes);
}
}
}
finally
{
content.Dispose();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment