Set TFS permission using Microsoft Outlook

The purpose for this post is to give step by step instructions to get user control TFS version control from outlook email, This is not intended just connect to version control; it is a way to connect to TFS while you are not available in your office, in my case I need to control giving TFS permissions from outside my office.

TFS Service

In this service I will cover these areas in TFS API

  • How to connect to Team foundation server
  • Setting permission in team foundation server version control items.

Now, create new C# console application in Visual studio,

Go to project from solution explorer and select add reference

Microsoft.TeamFoundation
Microsoft.TeamFoundation.Client
Microsoft.TeamFoundation.VersionControl.Client
System.Web.DomainServices

Add new method to connect to TFS in this example we will use the system user account to get TFS permission

private static bool ConnectToTFS()
{
// gets the credentials of the logged on user running the application

System.Net.NetworkCredential _credential = (System.Net.NetworkCredential)System.Net.CredentialCache.DefaultCredentials;

string _server = “http://TFSSERVER:8080″;

tfs = new TeamFoundationServer(_server, _credential);

tfs.Authenticate();

vcs = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));

return true;

}

Parsing the coming prams from string[] args

static void Main(string[] args)
{
//Add new array to simulate getting Parameters from command
//string[] args = new string[3];
//args[0] = “userID in AD,[path pr branch],[Grant or Remove],source control path”;
//args[1] = “Ayman”;
//args[2] = “Zaza”;

//Now you can add code to parse the incomming Args
//Add this code to set item permission in TFS, You can add any permission type , in my sample I will add //array contain {Read, Checkin } permission
string GroupName = DominName + “\\” + _userID;
string[] _groupCategoryPolicy ={PermissionChange.ItemPermissionRead,
PermissionChange.ItemPermissionCheckin
};

//And the connect to TFS, if connected check if the command to Grant or Remove Permissions  and then call //the ChangeItemPermissions method to do the action.
// if _PermissionType {Grant} grant permission.

if (_PermissionType.ToLower().ToString() == “Grant”){
bool status = ChangeItemPermissions(_path, GroupName,(string[])_groupCategoryPolicy, new string[] { }, new string[] { });
//if you want to send confirmation email you can call method to send email
SendResultEmail(_userID, SenderName, true, path);
}
// if _PermissionType {Remvoe} revoke the permission.
else if (_PermissionType.ToLower().ToString() == “Remove”)
{
bool status = ChangeItemPermissions(_path, GroupName, new string[] { }, new string[] { }, (string[])_groupCategoryPolicy);
//if you want to send confirmation email you can call method to send email
SendtEmail(userID, SenderName, false, path);
}}
{
if (ConnectToTFS())
//Add new method to set permission
private static bool ChangeItemPermissions(string path, string identity, string[] allows, string[] denies, string[] removes)
{
try{
PermissionChange _change = new PermissionChange(path, identity, allows, denies, removes);
vcs.SetPermissions(new SecurityChange[] { _change });
return true;
}

catch (Exception ex)

{return false;}
}

Now, build the Application and place it in public path to access it from outlook VBA.

Outlook Part

First I will add VAB code to get the email data

  • Open Microsoft Office Oultlook
  • Tools -> Micro-> visual basic editor [alt + F11]
  • Under modules, add new module and name it [GetMails]
  • Replace this code
Sub GetMails (MyMail As MailItem)

RetVal = Shell(”C:\ TFSOutlook.exe ” + Trim(MyMail.Body) + ” ” + Trim(MyMail.SenderName), 1)

End Sub
  •  Replace this method calls EXE File and passing parameters to exe [Mail Body],[Sender name] or you can add whatever you need t pass it.
  • Save and close the visual basic editor.
  • Go to Tools-> Rules and Alerts
  • Add new Rule.
  • Choose check message when they arrived image and click next.
  • From Rules wizard, check from people or distribution list and add your email or any email you want to start this service when you got specific email from.
  • Check with Specific words add for example [TFS123] and click next.
  • Check [run a script option] and in edit rule, choose [GetMails] module and click ok and next.
  • If you want to add any exceptions add it from next page.
  • Click finish to save it

Leave a Reply

Your email address will not be published. Required fields are marked *