Thursday, October 13, 2011

How to get changeset information using TFS API?

_________________________________________________________________________________

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.VersionControl.Client;

public void GetChangesetsInfo()
        {
            TfsTeamProjectCollection proj_coll = new TfsTeamProjectCollection(new Uri("http://NvnTfsserver:8080/tfs/defaultcollection/"), new System.Net.NetworkCredential(username, password));
            proj_coll.EnsureAuthenticated();

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

            //Following will get all changesets since 20 days. Note : "DateVersionSpec(DateTime.Now - TimeSpan.FromDays(20))"
            System.Collections.IEnumerable history = vcs.QueryHistory("$/Stakeholders Test Project", LatestVersionSpec.Instance, 0, RecursionType.Full, null, new DateVersionSpec(DateTime.Now - TimeSpan.FromDays(20)), LatestVersionSpec.Instance, Int32.MaxValue, false, false);

            foreach (Changeset changeset in history)
            {
                Console.WriteLine("Changeset Id: " + changeset.ChangesetId);
                Console.WriteLine("Owner: " + changeset.Owner);
                Console.WriteLine("Date: " + changeset.CreationDate.ToString());
                Console.WriteLine("Comment: " + changeset.Comment);
                Console.WriteLine("-------------------------------------");
            }
        }

 

Wednesday, October 12, 2011

How to get TFS Project groups and members information using TFS2010 APIs ?

-------------------------------------------------------------------------

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.VersionControl.Client;

 
public void PrintTFSGroupMembersInfo()
        {
            TfsTeamProjectCollection proj_coll = new TfsTeamProjectCollection(new Uri("http://NvnTFSServer:8080/tfs/defaultcollection/"), new System.Net.NetworkCredential(username, pass));
            proj_coll.Authenticate();

            VersionControlServer vcs = (VersionControlServer)proj_coll.GetService(typeof(VersionControlServer));
            TeamProject[] TPs = vcs.GetAllTeamProjects(false);

            IGroupSecurityService sec_groups = (IGroupSecurityService)proj_coll.GetService(typeof(IGroupSecurityService));


            foreach (TeamProject TP in TPs)
            {
                Console.WriteLine("Team Project : " + TP.Name);
                Console.WriteLine("Group Members : ");

                Identity[] app_groups = sec_groups.ListApplicationGroups(TP.ArtifactUri.ToString());

                foreach (Identity app_grp in app_groups)
                {
                    Identity grp = sec_groups.ReadIdentity(SearchFactor.Sid, app_grp.Sid, QueryMembership.Direct);

                    foreach (string members in grp.Members)
                    {
                        Identity member = sec_groups.ReadIdentity(SearchFactor.Sid, members, QueryMembership.Direct);

                        Console.WriteLine("     " + member.DisplayName);
                        foreach (string mm in member.Members)
                        {
                            Identity m = sec_groups.ReadIdentity(SearchFactor.Sid, mm, QueryMembership.Direct);

                            Console.WriteLine("         " + m.DisplayName);
                        }
                    }
                }


            }
        }