Get files from a Label

This post may be helpful for the people who are looking to get the content of a given label in a Workspace.

I looked for the code snippet on the internet unsuccessfully, to finally ask for help on the TFS Forum.


  VersionControlLabel[] labels = sourcecontrol.QueryLabels(labelname, "$/", tfs.AuthenticatedUserName, false, null, null, false);


  if (labels.Length>0)


    LabelVersionSpec lvs = new LabelVersionSpec(labels[0].Name);

    workspace.Get(lvs, GetOptions.None);


As you can see, it’s really simple once you know about the LabelVersionSpec class and that you can use it with the Workspace::Get(VersionSpec, GetOptions) method.

There are several classes that derive from the VersionSpec one:

·         ChangeSetVersionSpec, to get the content of a given changeset into your workspace.

·         DateVersionSpec, to get the content that were checked in until the given date.

·         LabelVersionSpec, to get from a label.

·         LatestVersionSpec, to get the latest version.

·         WorkspaceVersionSpec, to get the content of another Workspace.