/// <summary> /// Checks if a group exists and returns the descriptor. /// If the group does not exist, it is created (if the createIfNew flag is set to true). /// </summary> /// <param name="scope">Scope Id to limit the search area - default (empty string) is collection level.</param> /// <param name="groupName">Name of the group. Also used as group description for create.</param> /// <param name="createIfNew">Set to true for create. Default is false.</param> /// <returns>The IdentityDescriptor of the group. Returns null in case of any errors.</returns> private IdentityDescriptor CheckIfGroupExists(string scope, string groupName, bool createIfNew) { TeamFoundationIdentity tfi = null; try { // Find the group using Account Name tfi = idMgmtSvc.ReadIdentity(IdentitySearchFactor.AccountName, groupName, MembershipQuery.Direct, ReadIdentityOptions.None); if (tfi == null && createIfNew) { FileHelper.Log("Creating new group..." + groupName); // SCOPE: If a Team Project is found, add the group in that TP. // If not, add the group at collection level. // string scopeId = (teamProjectIdentity == null) ? string.Empty : teamProjectIdentity.Descriptor.Identifier; //string scopeId = tpCollection.Uri.ToString() + "/" + scope; IdentityDescriptor idDesc = idMgmtSvc.CreateApplicationGroup(scope, groupName, groupName); FileHelper.Log("Group creation successful..." + groupName); return(idDesc); } else { FileHelper.Log("Group identity found..." + groupName); return(tfi.Descriptor); } } catch (Exception ex) { FileHelper.Log(ex.Message); FileHelper.Log(ex.StackTrace); } return(null); }
/// <summary> /// Create TFS Group if not exists /// </summary> /// <param name="groupName"></param> /// <returns></returns> public TeamFoundationIdentity CreateGroup(string groupName) { // Check if group already exists TeamFoundationIdentity group = _identityManagementService .ListApplicationGroups(_projectInfo.Uri, ReadIdentityOptions.IncludeReadFromSource) .FirstOrDefault(g => string.Equals(g.DisplayName, groupName, StringComparison.OrdinalIgnoreCase)); if (group == null) { // Prepare group name var groupNameToCreate = groupName.Replace($"[{_teamProjectName}]\\", ""); // Group doesn't exist, create one var groupDescriptor = _identityManagementService.CreateApplicationGroup(_projectInfo.Uri, groupNameToCreate, null); group = _identityManagementService.ReadIdentity(groupDescriptor, MembershipQuery.None, ReadIdentityOptions.IncludeReadFromSource); Console.WriteLine($"Group created: '{groupName}'"); } else { Console.WriteLine($"Group already exists: '{groupName}'"); } return(group); }