How to get the list of valid users in a particular project?

i would like to get the valid users in my team server (with a particular project) and i will put it in a lets say, dropdown list. so that i can use it in adding workitems with the use of ASP.Net

i used the IGroupSecurityService but i can get only the Group Names and not the members inside the group.

any ideas Thanks,.

(btw, im using ASP.Net-C#)




Answer this question

How to get the list of valid users in a particular project?

  • Fernando Ronci

    Based on the Code Snippet of Robert Horvick I have build an example (C# Console Application)


  • DavidJW

    hi, this is what james manning did..

    but mine is different because it depends on the project and you can figure it out once you understood how it works.

    TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(servername/ip);

    IGroupSecurityService gss = (IGroupSecurityService)tfs.GetService(typeof(IGroupSecurityService));
    Identity idSID = gss.ReadIdentity(SearchFactor.AccountName, "Team Foundation Valid Users", QueryMembership.Expanded);
    Identity[] idUserName = gss.ReadIdentities(SearchFactor.Sid, idSID.Members, QueryMembership.None);

    foreach (Identity id in idUserName)
    {
    if (lstUsers.Contains(lstUsers.FindByText(id.DisplayName)) == false)
    {
    lstUsers.Add(id.DisplayName);
    }
    }



  • Okan Oksak

    Web access for adding/editing Work Items can be done with the Team Foundation Server API.

    Any Work Item belongs to a project. Each project has its security settings for Work Items.

    If you have the Team Explorer installed you can see the settings by selecting Team - Team Project Settings - Areas and Iterations and click on the 'Security ...' button. You'll discover that (by default) a project has 4 groups: Build Services, Contributors, Project Administrators and Readers. Only the first three groups can add/edit Work Items ('Edit work items in this node').

    To retrieve the list of Valid Users (Members of the groups Build Services, Contributors and Project Administrators) you'll have to use a TFS Web Service. Microsoft provides the Team Foundation Server API to make things easier.

    Based on the Code Snippet of Robert Horvick I have build an example (C# Console Application):

    using Microsoft.TeamFoundation;
    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.Server;
    using Microsoft.TeamFoundation.WorkItemTracking.Client;
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;
    
    namespace TFSValidUsers
    {
      class Program
      {
        static void Main(string[] args)
        {
          NextAllowedStateExample example = new NextAllowedStateExample();
    
          Console.Write("\nPress any key to close the application.");
          Console.ReadKey();
        }
      }
    
      class NextAllowedStateExample
      {
        public string TFSServername = "servername";
        public string teamProjectName = "projectname";
        
        private TeamFoundationServer tfs;
    
        public NextAllowedStateExample()
        {
          // connect to the Team Foundation Server
          ConnectToTFSServer();
          // retrieve the first Work Item
          WorkItem workItem = GetFirstWorkItem();
          // if the Work Item is succesfully retrieved
          if (workItem != null)
          {
            // show the next allowed states for this Work Item
            ShowValidUsers(workItem);
          }
        }
    
        public void ConnectToTFSServer()
        {
          Console.WriteLine("Connecting to TFS Server...");
          tfs = TeamFoundationServerFactory.GetServer(TFSServername);
          // authenticate with the current logged in user
          tfs.Authenticate();
        }
    
        public WorkItem GetFirstWorkItem()
        {
          Console.WriteLine("Retrieving Work Item...\n");
          WorkItem retWi = null;
          WorkItemStore store = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
    
          // Check if we have any projects
          if (store.Projects.Count <= 0)
            return retWi;
    
          // Get the proper Team Project
          Project project = store.Projects[0];
          if (project.StoredQueries.Count <= 0)
            return retWi;
    
          // query retrieves all work items for the specified team project
          string wiqlQuery = "SELECT [System.Id], [System.WorkItemType], [System.State], [System.AssignedTo], [System.Title] FROM WorkItems WHERE [System.TeamProject] = '" + teamProjectName + "' ORDER BY [System.WorkItemType], [System.Id]";
          // execute the query and retrieve a collection of workitems
          WorkItemCollection workitems = store.Projects[0].Store.Query(wiqlQuery);
          // loop through work items
          if (workitems.Count > 0)
          {
            // select first Work Item
            retWi = workitems[0];
          }
          return retWi;
        }
    
        public void ShowValidUsers(WorkItem wi)
        {
          WorkItem workItem = wi;
          // show basic information
          Console.WriteLine("Select Work Item {0} ({1})", workItem.Id.ToString(), workItem.Title);
    
          // get the Group Security Service
          IGroupSecurityService gsService = (IGroupSecurityService)tfs.GetService(typeof(IGroupSecurityService));
          // retrieve all application groups (by default: Contributors, Project Administrators, Build Services and Readers)
          // for the project this Work Item belongs to. Application groups do not contain member information.
          Identity[] applicationGroups = gsService.ListApplicationGroups(workItem.Project.Uri.AbsoluteUri);
          // for each application group
          foreach (Identity applicationGroup in applicationGroups)
          {
            // unless this is the Readers group (By default Readers do not have permissions to add/edit Work Items)
            if (applicationGroup.DisplayName != "Readers")
            {
              // retrieve the application group again (this time with members information)
              Identity[] groupContainer = gsService.ReadIdentities(SearchFactor.Sid, new string[] { applicationGroup.Sid }, QueryMembership.Expanded);
              // for each group (container contains only one group)
              foreach (Identity group in groupContainer)
              {
                // if this group has any members
                if (group.Members != null)
                {
                  // for each member in this group
                  foreach (string projectMemberSid in group.Members)
                  {
                    // get member(valid user) details
                    Identity validUser = gsService.ReadIdentity(SearchFactor.Sid, projectMemberSid, QueryMembership.None);
                    Console.WriteLine("valid user: {0}", validUser.DisplayName);
                  }
                }
              }
            }
          }
        }
      }
    }
    
    

  • MHerlund

    i still cant get it...

    i dont want to use my alternative way of doing it.. (getting the username and then validate it). i want it to be in a list...

    anyone tried and made this, that can help

    Thanks.



  • nolte

    its ok now.. i figured it out my self..(after tons of trials).. hehe

    and also i found out why there is no answer for this....

    Thanks.....



  • Moumen ITDev

    Can you share what you did to get the list of Valid users in a particular project, I am trying to do the same and would appreciated if you posted your code snippet.

    THanks.



  • How to get the list of valid users in a particular project?