Tuesday, December 22, 2009

Get User Permissions on TFS team project

This code is based on the listing in http://social.msdn.microsoft.com/Forums/en/tfsadmin/thread/9f977d37-23d8-4b07-a5dd-6cdc6d9dc6a6

public static List GetAccountPermissions(string account, string projectName)
{

List groupList = new List();
String serverUri = @"http://serverName:8080/";
// connect to the server
TeamFoundationServer server = new TeamFoundationServer(serverUri);
// get ICommonStructureService (later to be used to list all team projects)
ICommonStructureService iss = (ICommonStructureService)server.GetService(typeof(ICommonStructureService));
// get IGroupSecurityService (later to be used to retrieve identity information)
IGroupSecurityService2 gss = (IGroupSecurityService2)server.GetService(typeof(IGroupSecurityService2));
ProjectInfo[] allTeamProjects = iss.ListProjects();
// iterate thru the team project list
foreach (ProjectInfo pi in allTeamProjects)
{
// ProjectInfo gives you the team project's name, status, and URI.
String teamProjectName = pi.Name;
if (pi.Name == projectName)
{

Identity[] allProjectGroups = gss.ListApplicationGroups(pi.Uri);
// iterate thru the project group list and get a list of direct members for each project group
foreach (Identity projectGroup in allProjectGroups)
{
Identity pg = gss.ReadIdentity(SearchFactor.Sid, projectGroup.Sid, QueryMembership.Direct);

foreach (String groupMemberSid in pg.Members)
{
Identity m = gss.ReadIdentity(SearchFactor.Sid, groupMemberSid, QueryMembership.None);

if (m.AccountName == account)
{
groupList.Add(pg.DisplayName);
}
}
}
}
}
return groupList;
}

Thursday, December 17, 2009

Check String For Null

It's easy to forget but C# has a handy method to check if a string is null or empty:

If(!String.IsNullOrEmpty(str))
...do something

Wednesday, December 9, 2009

Get List of Allowed Values in TFS Work Item Field

This is how you can obtain programmatically a list of allowed values in a TFS Work Item field (in the example below, in Priority field in Bug template):

TeamFoundationServer tfs = new TeamFoundationServer("myTFServer");
WorkItemStore wis = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
Project teamProject = wis.Projects["myTeamProjectName"];
WorkItemType wit = teamProject.WorkItemTypes["Bug"];

FieldDefinition fd = wit.FieldDefinitions["Microsoft.VSTS.Common.Priority"];
AllowedValuesCollection avc = fd.AllowedValues;

foreach (var row in avc)
{
Console.WriteLine(row);
}

Monday, November 16, 2009

Get Current Date and Time in C#

DateTime.Now.ToShortDateString() + " "+ DateTime.Now.ToShortTimeString()

Friday, October 16, 2009

SQL Reporting Matrix Tips

Create a Percent Column in a Matrix

1) Create 2 text boxes in a column
Re: http://www.simple-talk.com/sql/reporting-services/advanced-matrix-reporting-techniques/









2) The first column will contain the standard sum expression : = Sum(Fields!DetaiField.Value)
DetailField is a placeholder for the field that you count on.

3) The second column will contain the following:
= Sum(Fields!DetaiField.Value)/
Sum(Fields!DetaiField.Value,"matrix_RowGroup")

Substitute the name of your row group for "matrix_RowGroup"
What we do here is sum up the count in the default scope divided by the total of items in a row.

4) The last step is to enclose the last expression into FormatPercent function:

FormatPercent(Sum(Fields!DetaiField.Value)/
Sum(Fields!DetaiField.Value,"matrix_RowGroup"))


Show a Variance Instead of a Total in the Subtotal Column

If you have a 2-column matrix, and you want to show a variance instead of the subtotal, you could use the following expression:

=Iif (inScope("matrix_ColumnGroupName"), Sum(Fields!DataField.Value),
Sum (IIF(Fields!ColumnName.Value = "Closed",
Fields!DataField.Value, Fields!DataField.Value * -1)))

First, you define an expression for the column data fields: Sum(Fields!DataField.Value).
The second part of the IIF function defines an expression for the Subtotal column. The value of the second data column becomes negative which allows us to obtain the variance instead of a sum. The expression assumes that the first column is named "Closed".

An example:

Date\State

Closed

Pending

Variance
8/17/2009 317 22 295
8/18/2009 5215 816 4399
9/14/2009 5526 503 5023
9/16/2009 49 3 46

Create a Variance Percentage Column

If instead of the variance you want to show a variance percentage, you need to use the following expression in the subtotal column created earlier:

FormatPercent (Sum (IIF(Fields!State.Value = "Closed", Fields!DataField.Value, Fields!DataField.Value * -1))/ Sum(Fields!DataField.Value)))

What you do here is obtain a variance value first, then a simple sum of all data fields in the row, and then divide the first value by the second.


Wednesday, August 26, 2009

Change Control Size Automatically with Form Size Change

Here is how you can change a form control size automatically when the form size is changed by user.

Declare a class variable that will hold the value of the current form size, and set its value in the contructor.

Size curSize;

public Form1()
{
InitializeComponent();
curSize = this.Size;
}


To capture form size change, use the ClientSizeChanged event handler with the following code:

private void Form1_ClientSizeChanged(object sender, EventArgs e)
{

int heightDif = Size.Height - curSize.Height;
int widthDif = Size.Width - curSize.Width;
yourControlName.Height += heightDif;
yourControlName.Width += widthDif;
//store the current size until the next time this event is raised
curSize = this.Size;
}

Tuesday, August 18, 2009

C# Interview Questions

Some links to C# interview questions as based on Google search. The usefulness of the questions improves as you go down the list.

http://blogs.crsw.com/mark/articles/252.aspx

http://www.techinterviews.com/c-shrp-interview-questions-and-answers

Questions for a senior C# developer -

http://www.hanselman.com/blog/WhatGreatNETDevelopersOughtToKnowMoreNETInterviewQuestions.aspx

The following one is more interesting -

http://dev.fyicenter.com/Interview-Questions/C-sharp/


http://www.coolinterview.com/interview/8990/

http://venkatcsharpinterview.blogspot.com/2009/01/c-interview-questions.html

http://www.dotnetfunda.com/interview/ShowCatQuestion.aspx?category=34