Click here to Skip to main content
15,886,026 members
Articles / Programming Languages / C#

VSS Helper Addin for VSTS 2005

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
2 Feb 2009CPOL4 min read 25.1K   122   5   2
VSS Addin for Visual Studio 2005 - to get check out information on a file
Image 1

Introduction

VSS Helper- Addin for VSTS 2005 is an addin tool to interact with VSS from the Visual Studio IDE. Currently, Visual Studio IDE doesn't provide information about who has checked out a file apart from the current user. It is always time-consuming for the developer to identify who has checked out the file which is required by him/her. VSS Helper gives quick information on this and helps the developer recover this mess, and that too inside VSTS itself.

Background

Most of the .NET projects today are developed using VSTS and VSS as configuration tool. The teams work on different files and do the development. VSTS has already integrated with VSS, wherein the user can check out and check in the file from the IDE. The challenge arises when more than one person wants to work on the same file. VSTS IDE only indicates whether a file has been checked out or not in the solution explorer. The developer will not know who has checked out the file in VSTS, until he goes to VSS and  gets the information. ‘VSS helper’ addin comes handy with this functionality and fits snugly within VSTS. This greatly saves the development time for the developers.

This addin tool can be used with any project which suffices the following 2 conditions:

  • Using VSTS 2005 for development
  • Using VSS as configuration management tool.

Using the Code

I am using the VSS API  (ssapi.dll) to get the information from VSS. This API has to be added as reference before running this project. To get the VSS Database path and login details for VSS, an XML file is used. The syntax for the XML file is given below. A sample XML file is also attached with this article.

XML
<xml version="1.0" >
 <VSSHelper Databasepath ="#Your VSS database path#" 
	UserName="#user name#" Password="#password#" 
	DefaultRootFolder="#Starting folder#"</VSSHelper>
</xml>

The default root folder is the place which is shown to the user when the application starts.

C#
// Declaring variables
private VSSDatabase vssDatabase = new VSSDatabase();
private bool IsVSSConnected = false;
VSSItem Item; DataSet ds = new DataSet();
private string projectPath = string.Empty;
FileObject fObject = new FileObject();
private string database;
private string userName;
private string password;
private string defaultPath;
private string filePath;
//Change the XML file path where you have saved it
private string xmlFilePath = @"C:\XMLFile2.xml"; 

On the form load, I get the VSS details from the XML file:

C#
public Form1()
{ try
   {
     ReadXML();
     vssDatabase.Open(this.database, this.userName,this.password);
     InitializeComponent();
     txtVSSFilePath.Text = this.defaultPath;
   }

   catch (Exception ex)
   {
	MessageBox.Show(this, ex.Message, "VSS Helper - Error connecting to VSS");
	this.Dispose();
   }
 }

//Get the information from the XML
 private void ReadXML()
{
  XmlDocument doc = new XmlDocument();
  doc.Load(xmlFilePath);
  XmlElement root = doc.DocumentElement;
  this.database = root.SelectNodes(@"VSSHelper")[0].Attributes[0].Value;
  this.userName = root.SelectNodes(@"VSSHelper")[0].Attributes[1].Value;
  this.password = root.SelectNodes(@"VSSHelper")[0].Attributes[2].Value;
  this.defaultPath = root.SelectNodes(@"VSSHelper")[0].Attributes[3].Value;
 } 

The following function is called to perform connection with VSS and populate the tree view control. (This tree view control visibility is set to false and is used internally to get VSS file path information.)

C#
 //Perform connection to VSS
 private void ConnectToVSSDatabase()
 {
  Item = vssDatabase.get_VSSItem(projectPath, false);
  TreeNode node = new TreeNode();
  node.Text = projectPath;
  foreach (VSSItem VSSItem1 in Item.get_Items(false))
  {
    TreeNode newNode = new TreeNode();
    //If it is a project and then loop to get all the files
    if (VSSItem1.Type == (int)VSSItemType.VSSITEM_PROJECT) 
    {
      string ChildProjectPath = VSSItem1.Spec + "/";
      newNode.Text = VSSItem1.Name;
      newNode.Name = VSSItem1.Name;
      getFiles(ChildProjectPath, ref newNode);
    }
    else //Then it is file
    {
      newNode.Text = VSSItem1.Name;
      newNode.Name = VSSItem1.Name;
      //If file has checked out
      if (VSSItem1.IsCheckedOut > 0)
      {
	newNode.Name = newNode.Text;
	//All the checked out files will be prefixed with a "~"
	newNode.Text = "~" + newNode.Text; 
      }
    } node.Nodes.Add(newNode);
  }
}

//To get files from a project
private void getFiles(string itemname, ref TreeNode node)
{
 VSSItem ItemChild = vssDatabase.get_VSSItem(itemname, false);
  foreach (VSSItem VSSItem1 in ItemChild.get_Items(false))
    {
      if (VSSItem1.Type == (int)VSSItemType.VSSITEM_FILE)
      {
   	TreeNode newNode = new TreeNode();
	//If file has checked out
	if (VSSItem1.IsCheckedOut > 0)
	{
           newNode.Text = "~" + VSSItem1.Name; newNode.Name = VSSItem1.Name;
	}
	else
	{
           newNode.Text = VSSItem1.Name;
  	  newNode.Name = VSSItem1.Name;
	}
	node.Nodes.Add(newNode);
      }
      else if (VSSItem1.Type == (int)VSSItemType.VSSITEM_PROJECT) //this is 0
      {
	string ChildProjectPath = VSSItem1.Spec + "/";
	TreeNode newNode = new TreeNode();
	newNode.Text = VSSItem1.Name;
	newNode.Name = VSSItem1.Name;
	node.Nodes.Add(newNode);
	getFiles(ChildProjectPath, ref newNode);
      }
   }
} 

The following function shows how to get the VSS file path from the tree view using Tree node find function. Note that the same file can be in different projects. Hence I get all the collections in the array. The details are then stored in a dataset which is finally binded to the grid. This grid is displayed to the user.

C#
//Given a local file name if it has been checked out, 
//list the name on which it has been checked out

private void GetCheckOutByPath()
{
//Use the tree node. Find method to get the file path details
 TreeNode[] nodeArray = treeView1.Nodes.Find(filePath, true); 
 string path = string.Empty;
 FileObject fNewObject = new FileObject();
 if (nodeArray.Length > 0)
 {
   foreach (TreeNode node in nodeArray)
   {
    path = node.FullPath.ToString();
    fNewObject = CheckOutBy(path);
    if (fNewObject != null)
    {
      DataRow dr = ds.Tables[0].NewRow();
      dr[0] = fNewObject.FileName; //File name
      dr[1] = fNewObject.FilePath; //File path in VSS
      dr[2] = fNewObject.CheckedOutBy; //Checked out information
      ds.Tables[0].Rows.Add(dr);
    }
  }
 }
}

The addin also searches if the file name is partly given with wild char (*). The following functions are used to get the file details. These functions are called based on the position of * in the file name.

C#
//Search with starting character match
private void RecursiveStartSearch(TreeNodeCollection nodeCollection)
{
 string path = string.Empty;
 FileObject fNewObject = new FileObject();
 foreach (TreeNode node in nodeCollection)
  {
    if (node.Name.ToLower().StartsWith(filePath))
    //Search for the files which match the starting character
    {
     path = node.FullPath.ToString();
     fNewObject = CheckOutBy(path);
     //See if it is checked out
     if (fNewObject != null)
       {
         DataRow dr = ds.Tables[0].NewRow();
         dr[0] = fNewObject.FileName;
         dr[1] = fNewObject.FilePath;
         dr[2] = fNewObject.CheckedOutBy;
         ds.Tables[0].Rows.Add(dr);
       }
    } //Loop again for subsequent nodes
    if (node.GetNodeCount(true) > 0)
      this.RecursiveStartSearch(node.Nodes);
 }
}

To search with ending character match, use the following function.  The same function can be extended to list out all checked out files.

C#
//Search with ending character match.
private void RecursiveEndSearch(TreeNodeCollection nodeCollection)
{
string path = string.Empty;
FileObject fNewObject = new FileObject();
foreach (TreeNode node in nodeCollection)
{
  if (node.Name.ToLower().EndsWith(filePath))
   //Search the node which ends with local file name
    {
      path = node.FullPath.ToString();
      fNewObject = CheckOutBy(path);
      //See if it is checked out
      if (fNewObject != null)
        {
           DataRow dr = ds.Tables[0].NewRow();
           dr[0] = fNewObject.FileName;
           dr[1] = fNewObject.FilePath;
           dr[2] = fNewObject.CheckedOutBy;
           ds.Tables[0].Rows.Add(dr);
       }
    }
   //Loop again for subsequent nodes
   if (node.GetNodeCount(true) > 0)
   this.RecursiveEndSearch(node.Nodes);
 }
} 

Create a new addin in VSTS using the VSTS Addin template wizard. Call this form in the Exec method in the connect.cs file.

Points of Interest

When the user performs the connection with VSS, I read the entire VSS (from the root folder given in XML) and build a tree view. To get check out information for a file, I used the Tree node find method to get quick information on the VSS file path. This greatly saved time for me to get the check out details. On the other side, for any new files added into VSS, the connection has to be performed again to build the tree view.

History

  • 02-Oct-08: Created
  • 18-Nov-08: Resolved the problem of back slash if not found in the VSS file path. Added ConvertSlash function. Replaced form click event for About information to ? mark. Added a new label for this.
  • 21-Nov-08: Fixed the following Form Resize issue. Moved the XML file to C:\ Modifying the VSS path when the user clicks FIND Alerting the user when VSS connection is not performed.
  • 27-Nov-08: Fixed the following: Modified the form style. Disabling the CONNECT and FIND button while performing connection. Added WORKING status in the title bar when connection is being made.
  • 29-Dec-08: Included Wild char search (*). Also added thread while searching so user can see "Working status when search is performed".

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer MNC
India India
I am currently working as programmer in a software company. I focus mostly on Microsoft Products and currently in dot net framework 3.5.

Comments and Discussions

 
QuestionHow do you use your addin? [modified] Pin
redjoy3-Feb-09 9:43
redjoy3-Feb-09 9:43 
AnswerRe: How do you use your addin? [modified] Pin
Senthil Kumeresh29-Apr-14 5:30
Senthil Kumeresh29-Apr-14 5:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.