Parts were taken from two posts.
Post1
Post2
These bits of code are actually part of something I wrote this morning and may be something others wish to use or it may help them.
edit: many edits for cleaning code up.
Code: Select all
using System;
using System.Collections.Generic;
using VaultClientIntegrationLib;
using VaultClientOperationsLib;
using VaultLib;
/// <summary>
/// Grants code level access for source control.
/// </summary>
public class Vault
{
#region Constant Fields
#endregion Constant Fields
#region Fields
/// <summary>
/// Shows if a user is logged in.
/// </summary>
private static bool isLoggedIn = false;
/// <summary>
/// Shows which user is currently logged in. Null if none.
/// </summary>
private static string usernameLoggedIn = null;
/// <summary>
/// Shows the working folder. Null if unassigned.
/// </summary>
private static string workingFolder = null;
#endregion Fields
#region Constructors
#endregion Constructors
#region Finalizers (Destructors)
#endregion Finalizers (Destructors)
#region Delegates
#endregion Delegates
#region Events
#endregion Events
#region Enums
#endregion Enums
#region Interfaces
#endregion Interfaces
#region Properties
/// <summary>
/// Gets a value indicating whether the user is logged in.
/// </summary>
public static bool IsUserLoggedIn
{
get
{
return isLoggedIn;
}
}
/// <summary>
/// Gets the curreny logged in user name.
/// </summary>
public static string UserNameLoggedIn
{
get
{
return usernameLoggedIn;
}
}
/// <summary>
/// Gets the current working folder.
/// </summary>
public static string WorkingFolder
{
get
{
return workingFolder;
}
}
#endregion Properties
#region Indexers
#endregion Indexers
#region Methods
/// <summary>
/// Gets the full source tree.
/// </summary>
public static void GetFullSourceTree()
{
GetOperations.ProcessCommandGet(new string[] { workingFolder }, new GetOptions());
}
/// <summary>
/// Given a path, return all the labels from that path.
/// </summary>
/// <param name="path">Path to search in. We're expecting the format of: $/Dingo</param>
/// <param name="includeInherited">Inherited items are items parents have.</param>
/// <param name="includeFolderItems">Includes folder items or not. You most likely want this.</param>
/// <param name="enableRecursiveSearch">This will return child labels.</param>
/// <returns>List of all labels found in that path.</returns>
public static List<string> GetLabelFromPath(string path, bool includeInherited, bool includeFolderItems, bool enableRecursiveSearch)
{
List<string> labelList = new List<string>();
int rowsInheritedCount;
int rowsRecursiveCount;
string token = String.Empty;
VaultLabelItemX[] items = null;
VaultClientTreeObject tree = RepositoryUtil.FindVaultTreeObjectAtReposOrLocalPath(path);
ServerOperations.client.ClientInstance.BeginLabelQuery(tree.FullPath, tree.ID, enableRecursiveSearch, includeInherited, true, includeFolderItems, 1000, out rowsInheritedCount, out rowsRecursiveCount, out token);
if (token == null || token == String.Empty)
{
throw new Exception("Token null. I have no idea what this means but you should have some pie.");
}
ServerOperations.client.ClientInstance.GetLabelQueryItems_Main(token, 0, rowsInheritedCount, out items);
if (items == null)
{
/* Most likely nothing was in the list. Instead of returning an empty array, null is returned. */
return labelList;
}
foreach (VaultLabelItemX item in items)
{
labelList.Add(item.Name);
}
ServerOperations.client.ClientInstance.EndLabelQuery(token);
return labelList;
}
/// <summary>
/// Gets a tree from a label given a path.
/// </summary>
/// <param name="sourcePath">The path to the label. For example $/Development/</param>
/// <param name="labelName">Name of the label.</param>
public static void GetSourceTreeFromLabel(string sourcePath, string labelName)
{
string destPath = workingFolder + "\\" + labelName;
GetOperations.ProcessCommandGetLabelToTempWorkingFolder(sourcePath, labelName, null, new GetOptions(), destPath);
}
/// <summary>
/// Logs a user in. You only need to call this once in a session.
/// </summary>
/// <param name="serverUrl">URL to login.</param>
/// <param name="userName">Username to login under.</param>
/// <param name="password">Password to that user.</param>
/// <param name="repository">The repository to open. We currently use: Fellowship Repos</param>
public static void Login(string serverUrl, string userName, string password, string repository, string workingFolder)
{
ServerOperations.client.LoginOptions.URL = serverUrl;
ServerOperations.client.LoginOptions.User = userName;
ServerOperations.client.LoginOptions.Password = password;
ServerOperations.client.LoginOptions.Repository = repository;
ServerOperations.Login();
isLoggedIn = true;
usernameLoggedIn = userName;
SetRootWorkingFolder(workingFolder);
}
/// <summary>
/// Logs a user out of the session.
/// </summary>
public static void Logout()
{
ServerOperations.Logout();
isLoggedIn = false;
usernameLoggedIn = null;
}
/// <summary>
/// Sets the root working folder.
/// </summary>
/// <param name="localFolder">Location on the local drive. In Vista and newer operating systems, it's recommended to use something inside of c:\Users\%username% to keep it out of the root and to avoid unnecessary permissions. You can use something like Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments). Remember, paths too deep causes Visual Studio 2008 to not be happy. 256 chars max for path+projects. Some areas care less, some care more. Tread lightly.</param>
public static void SetRootWorkingFolder(string localFolder)
{
ServerOperations.SetWorkingFolder("$/", localFolder, true);
workingFolder = localFolder;
}
#endregion Methods
#region Structs
#endregion Structs
#region Classes
#endregion Classes
}