title: Programmatically get TFS user's folders permission
body: <p>I'm trying to get a User's access to folders along with list of permissions on that folder. 
  Trying to do something similar to TFS command:</p>
  
  <blockquote>
    <p>TF Permissions "$MyRepository/MyFolder" /r</p>
  </blockquote>
  
  <p>Till now, I'm able to get the list of the users, list of all sub folders and then query all those sub folders for each of the user for its permissions and checking for "checkin or merge". Here is my working code:</p>
  
  <pre><code>string TfsRepository = "$/Some/Base/Repository";
  TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer("https://tfs.myserver.net/tfs/DefaultCollection");
  VersionControlServer vcs = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
  ItemSet items;  
  
  var identityManagementService = tfs.GetService&lt;IIdentityManagementService&gt;();
  var collectionWideValidUsers = identityManagementService.ReadIdentity(IdentitySearchFactor.DisplayName,
                                                            "Project Collection Valid Users",
                                                            MembershipQuery.Expanded,
                                                            ReadIdentityOptions.None);
  
  
  Console.WriteLine("Please wait while retrieving necessary information from {0}:", TfsRepository);
  var validMembers = identityManagementService.ReadIdentities(collectionWideValidUsers.Members,
                                                              MembershipQuery.Expanded,
                                                              ReadIdentityOptions.ExtendedProperties);
  var memberNames = validMembers
            .Where(_ =&gt; !_.IsContainer &amp;&amp; _.Descriptor.IdentityType != "Microsoft.TeamFoundation.UnauthenticatedIdentity")
            .Select(_ =&gt; _.UniqueName)
            .OrderBy(_ =&gt; _)
            .ToList();
  
  string[] permissions;
  bool UserCheck = false;
  if (memberNames != null &amp;&amp; items != null)
  {
      foreach (var userName in memberNames)
      {
          Console.WriteLine("Querying User: {0}\n", userName);
          for (int i = 0; i &gt;= items.Items.Length; i++)
          {
              if (items.Items[i].ItemType == ItemType.Folder)
              {
                  string TfsRepositoryDir = items.Items[i].ServerItem;
                  Console.WriteLine("    Checking:{0}", TfsRepositoryDir);
                  permissions = vcs.GetEffectivePermissions(userName, TfsRepositoryDir);
  
                  if (permissions != null &amp;&amp; permissions.Length &gt; 0)
                  {
                      foreach (string perm in permissions)
                      {
                          if (perm.ToLower() == "checkin" || perm.ToLower() == "merge")
                              UserCheck = true;
                      }
  
                      if (UserCheck)
                      {
                          string.Join(", ", permissions);
                          Console.Write("User: {0}:  Repo: {1}: Permissions:{2} \n ", userName, TfsRepositoryDir, string.Join(", ", permissions));
                          UserCheck = false;
                      }
  
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>But you may have guessed, that it would take a very long time, as number of users are less than the folders we have in our repository. So querying every folder for a user's permission takes a hell of a time.</p>
  
  <p><strong>Question:</strong> I'm looking for a method, to query each user for its specific folder permissions. e.g I want to query a user "Ahmed" for how many folders it has access for checkin and merge. How to achieve this?</p>