C# code to find SharePoint web application Group's.
  1. public List<NameValuePair> GetGroups(string siteUrl)
  2. {
  3. List<NameValuePair> groups = new List<NameValuePair>();

  4. using (SPSite site = new SPSite(siteUrl))
  5. {
  6. using (SPWeb web = site.OpenWeb())
  7. {
  8. foreach (SPGroup group in web.Groups)
  9. {
  10. groups.Add(new NameValuePair() { Value = group.Name, Name = group.Name });
  11. }
  12. }
  13. }
  14. return groups;
  15. }
C# code to find SharePoint web application Group owner names.
  1. public List<NameValuePair> GetGroupsWithOwner(string siteUrl)
  2. {
  3. List<NameValuePair> groups = new List<NameValuePair>();

  4. using (SPSite site = new SPSite(siteUrl))
  5. {
  6. using (SPWeb web = site.OpenWeb())
  7. {
  8. string ownerName = string.Empty;
  9. foreach (SPGroup group in web.Groups)
  10. {
  11. if (group.Owner is SPUser)
  12. {
  13. ownerName = (group.Owner as SPUser).Name;
  14. }
  15. else if (group.Owner is SPGroup)
  16. {
  17. ownerName = (group.Owner as SPGroup).Name;
  18. }
  19. groups.Add(new NameValuePair() { Value = group.Name, Name = string.Format("{0} ({1})", group.Name, ownerName) });
  20. }
  21. }
  22. }
  23. return groups;
  24. }
C# code to remove specific group from given web application.
  1. public List<NameValuePair> RemoveGroups(List<string> groups, string siteUrl)
  2. {
  3. List<NameValuePair> groupstatus = new List<NameValuePair>();
  4. using (SPSite site = new SPSite(siteUrl))
  5. {
  6. using (SPWeb web = site.OpenWeb())
  7. {
  8. NameValuePair groupstat;
  9. foreach (string group in groups)
  10. {
  11. groupstat = new NameValuePair();
  12. try
  13. {
  14. web.Groups.Remove(group);
  15. groupstat.Name = string.Format("{0} removed sucessfully.", group);
  16. groupstat.Value = true;
  17. }
  18. catch (Exception ex)
  19. {
  20. groupstat.Name = string.Format("Error {1} while removing gorup {0}", group, ex.Message);
  21. groupstat.Value = false;
  22. }
  23. groupstatus.Add(groupstat);
  24. }
  25. }
  26. }
  27. return groupstatus;
  28. }
C# code to add user to web application.
  1. public bool AddUserToGroup(string siteUrl, string groupName, string memberName)
  2. {
  3. bool isOwnerUpdated = false;
  4. using (SPSite site = new SPSite(siteUrl))
  5. {
  6. using (SPWeb web = site.OpenWeb())
  7. {
  8. try
  9. {
  10. SPUser user = web.EnsureUser(memberName);
  11. SPGroup group = web.Groups[groupName];
  12. group.AddUser(user);
  13. group.Update();
  14. }
  15. catch (Exception ex)
  16. {
  17. }
  18. }
  19. }
  20. return isOwnerUpdated;
  21. }
C# code to set owner for web application.
  1. public bool SetOwnerToGroup(string siteUrl, string groupName, string ownerName, bool isGroup)
  2. {
  3. bool isOwnerUpdated = false;
  4. using (SPSite site = new SPSite(siteUrl))
  5. {
  6. using (SPWeb web = site.OpenWeb())
  7. {
  8. try
  9. {
  10. SPGroup group = web.Groups[groupName];
  11. SPMember owner;
  12. owner = web.Groups[ownerName];
  13. group.Owner = owner;
  14. group.Update();
  15. }
  16. catch (Exception ex)
  17. {
  18. isOwnerUpdated = false;
  19. }
  20. }
  21. }
  22. return isOwnerUpdated;
  23. }
C# code to get group members.
  1. public List<NameValuePair> GetGroupMembers(string siteUrl, string groupName)
  2. {
  3. List<NameValuePair> groupMembers = new List<NameValuePair>();
  4. using (SPSite site = new SPSite(siteUrl))
  5. {
  6. using (SPWeb web = site.OpenWeb())
  7. {
  8. NameValuePair groupMember;
  9. SPGroup group = web.Groups[groupName];
  10. foreach (SPUser user in group.Users)
  11. {
  12. groupMember = new NameValuePair();
  13. groupMember.Value = groupMember.Name = user.Name;
  14. groupMembers.Add(groupMember);
  15. }
  16. }
  17. }
  18. return groupMembers;
  19. }
C# code to check valid users.
  1. public bool IsValidUser(string UserName, SPWeb web)
  2. {
  3. SPAdministrationWebApplication centralAdmin = SPAdministrationWebApplication.Local;
  4. SPPrincipalInfo principle = SPUtility.ResolvePrincipal(centralAdmin, SPUrlZone.Default, UserName,
  5. SPPrincipalType.User, SPPrincipalSource.All, false);
  6. //( web , SPUrlZone.Default, UserName, SPPrincipalType.User, SPPrincipalSource.All, false);
  7. if (principle == null)
  8. return false;
  9. return true;
  10. }