TFS API Part 38 – Create Label

14/06/2011



In my last post on TFS API I summarize in Index Page include 37 parts –http://blogs.microsoft.co.il/blogs/shair/archive/2011/05/18/tfs-api-index-parts-1-to-37.aspx


And I coming back with more about TFS API, and Today about how to create Label using TFS API.


Download Demo Project


image


image



Step 1: Create Project and Add Reference


Create an WPF/WinForm application and add the following references:


First add reference for



  • Microsoft.TeamFoundation.dll

  • Microsoft.TeamFoundation.Build.Client

  • Microsoft.TeamFoundation.Build.Common.dll

  • Microsoft.TeamFoundation.dll

All files located under – c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\


Step 2: Connect Team Foundation Server


for creating Label the only object we need to obtain is VersionControlServer, everything we need is there.

private void BtnConnectClick(object sender, RoutedEventArgs e)
{     var dp = new TeamProjectPicker(TeamProjectPickerMode.NoProject, false);     dp.ShowDialog(); //Display DomainProjectPicker     if (dp.SelectedTeamProjectCollection != null)     {         tfs = dp.SelectedTeamProjectCollection;         //Create VersionControlServer object from TFS         sourceControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));         bg.RunWorkerAsync();     }
}

Step 3: Map Source Control

I’ve already talked about this in the past, so please check this post – TFS API Part 16: Mapping Source Control Using VersionControlServer

(Also in the Demo Project)


Step 4: Create Label


And for the important part, when we draw the source control tree we assigned the Server Path ($/Project/Folder etc) as Tag – and this is very important because this is our Label Scope.


We will start with creating VersionControlLabel contains the VersionControlServer object, Label Name, Owner and Comments.


Then we need to define the ItemSpec for the label – Which Files, Folder we want to Label.


And Finally using VersionControlLabel.CreateLabel with our settings.

private void btnCreateLabel_Click(object sender, RoutedEventArgs e)
{     if (SourceTree.SelectedItem == null || string.IsNullOrEmpty(txtLabelName.Text) ||       
   comboLabelChildOption.SelectedItem == null || comboRecursion.SelectedItem == null) return;     
    //get the full path from the selected file\folder   
var path = ((TreeViewItem)SourceTree.SelectedItem).Tag.ToString(); 
    //Represents a version control label.   
  var versionControlLabel = new VersionControlLabel(sourceControl, txtLabelName.Text, sourceControl.AuthorizedUser,path, txtComments.Text);
    //Represents one or more files or folders on the local machine or in the repository.
   var itemSpec = new ItemSpec(path, (RecursionType)comboRecursion.SelectedItem); 
    // What items should this label apply to? 
   var labelItemSpec = new LabelItemSpec[1]; 
    labelItemSpec[0] = new LabelItemSpec(itemSpec, VersionSpec.Latest, false); 
    //*** LabelChildOption ***   
//If you use Replace, any item that you specify in your request that’s already in the label will be  
    //replaced by what you have specified. Anything that’s not in the label already will be added to   
   //the label, and existing items that aren’t modified by your request will remain as they were.    
//Use Merge to leave unmodified items that are in your request but that already exist in the label. 
    //Any item in your request that’s not already in the label will be added to the label, and existing
    //items in the label that aren’t specified in your label will remain unchanged. Explaining the Fail    
//option is not easy. Labels have the notion of a scope. The Fail option says that the call to create    
//or update the label should fail if there is at least one label by the same name that exists at a     
//scope that would be below the scope of the new label.    
    var lb = sourceControl.CreateLabel(versionControlLabel, labelItemSpec, (LabelChildOption)comboLabelChildOption.SelectedItem); 
    MessageBox.Show(lb[0].Status.ToString());
}
Download Demo Project
Enjoy.

Add comment
facebook linkedin twitter email

Leave a Reply