TFS API Part 32 – Add and Remove Users From Application Groups

24/11/2010

TFS API Part 32 – Add and Remove Users From Application Groups

imageimage

Download Demo Project

Step 1: Create Project and Add Reference

Create an WPF/WinForm application and add the following references:

First add reference for

  • Microsoft.TeamFoundation.dll
  • Microsoft.TeamFoundation.Client.dll
  • Microsoft.TeamFoundation.Common.dll

All files located under – c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\

Step 2: Connect to Team Foundation Server

(TFS API Part 20: Bye TeamFoundationServer and Welcome TfsTeamProjectCollection)

IGroupSecurityService gss;
private void button1_Click(object sender, RoutedEventArgs e)
{
    TeamProjectPicker tpp = new TeamProjectPicker(TeamProjectPickerMode.SingleProject, false);
    tpp.ShowDialog();

    if (tpp.SelectedProjects.Length > 0)
    {
        TFS = tpp.SelectedTeamProjectCollection;
        Project = tpp.SelectedProjects[0];
        gss = (IGroupSecurityService)TFS.GetService(typeof(IGroupSecurityService));
    }
}

Step 3: Get All Users and Groups by Project

First create listbox called “list_users” to contain the users list and a combobox called “combo_groups” to contain the Project Groups

void GetUsers()
{
    //Perfom search based on "Project Collection Valid Users" this valid only for TFS 2010 in order to user the same functionality on TFS 2008
    //You need to user Team Foundation Valid Users"
    Identity SIDS = gss.ReadIdentity(SearchFactor.AccountName, "Project Collection Valid Users", QueryMembership.Expanded);
    Identity[] UserIds = gss.ReadIdentities(SearchFactor.Sid, SIDS.Members, QueryMembership.None);

    var query = from user in UserIds
                where user.Type == IdentityType.WindowsUser
                select user;

    list_users.ItemsSource = query;
}

void GetGroups()
{
    Identity SIDS = gss.ReadIdentity(SearchFactor.EveryoneApplicationGroup, "", QueryMembership.Expanded);
    Identity[] groups = gss.ReadIdentities(SearchFactor.Sid, SIDS.Members, QueryMembership.None);

    var query = from _group in groups
                where _group.Type == IdentityType.ApplicationGroup
                select _group;

    combo_groups.ItemsSource = gss.ListApplicationGroups(Project.Uri);
}

Step 4: Get User List by Group

After you select a group from the combobox this method will obtain all Users from that application group.

void DefineGroupUsers()
{
    if (combo_groups.SelectedItem != null)
    {
        Identity group = combo_groups.SelectedItem as Identity;

        //Perform new search based on Sid and from the selected Group.
        Identity SIDS = gss.ReadIdentity(SearchFactor.Sid, group.Sid, QueryMembership.Expanded);
        //Read the users related to that group.
        if (SIDS.Members.Length > 0)
        {
            Identity[] UserIds = gss.ReadIdentities(SearchFactor.Sid, SIDS.Members, QueryMembership.None);
            list_group_users.ItemsSource = UserIds;
        }
        else
            list_group_users.ItemsSource = null;
    }
}

Step 5: Add and Remove User From Application Group

Now we getting to the important part – Adding and Removing Users from Groups.

User IGroupSecurityService object this is very easy task – just call RemoveMemberFromApplicationGroup method to remove user from group with the group Sid and User Sid, same goes for Adding a User.

private void btn_remove_Click(object sender, RoutedEventArgs e)
{
    if (list_group_users.SelectedItem != null && combo_groups.SelectedItem != null)
    {
        Identity user = list_group_users.SelectedItem as Identity;
        Identity group = combo_groups.SelectedItem as Identity;
        //Remove User to Specific Application Group
        gss.RemoveMemberFromApplicationGroup(group.Sid, user.Sid);
        //Refresh Users List
        DefineGroupUsers();
    }
}

private void btn_add_Click(object sender, RoutedEventArgs e)
{
    if (combo_groups.SelectedItem != null && list_users.SelectedItem != null)
    {
        Identity user = list_users.SelectedItem as Identity;
        Identity group = combo_groups.SelectedItem as Identity;
        //Add User to Specific Application Group
        gss.AddMemberToApplicationGroup(group.Sid, user.Sid);
        //Refresh Users List
        DefineGroupUsers();
    }
}

Download Demo Project

Enjoy

Add comment
facebook linkedin twitter email

Leave a Reply

2 comments

  1. Crispin Parker06/12/2010 ב 14:46

    Excellent clear instructions as always! Thanks.

  2. Leslie03/11/2011 ב 12:55

    Hi,i have one problem .
    So ,how can i add users which are not from Application Groups?