C# code to find SharePoint web application Group's.
- public List<NameValuePair> GetGroups(string siteUrl)
- {
- List<NameValuePair> groups = new List<NameValuePair>();
- using (SPSite site = new SPSite(siteUrl))
- {
-
- using (SPWeb web = site.OpenWeb())
- {
- foreach (SPGroup group in web.Groups)
- {
- groups.Add(new NameValuePair() { Value = group.Name, Name = group.Name });
- }
- }
- }
-
- return groups;
- }
C# code to find SharePoint web application Group owner names.
- public List<NameValuePair> GetGroupsWithOwner(string siteUrl)
- {
- List<NameValuePair> groups = new List<NameValuePair>();
- using (SPSite site = new SPSite(siteUrl))
- {
-
- using (SPWeb web = site.OpenWeb())
- {
- string ownerName = string.Empty;
- foreach (SPGroup group in web.Groups)
- {
-
- if (group.Owner is SPUser)
- {
- ownerName = (group.Owner as SPUser).Name;
- }
- else if (group.Owner is SPGroup)
- {
- ownerName = (group.Owner as SPGroup).Name;
- }
- groups.Add(new NameValuePair() { Value = group.Name, Name = string.Format("{0} ({1})", group.Name, ownerName) });
- }
- }
- }
-
- return groups;
- }
C# code to remove specific group from given web application.
- public List<NameValuePair> RemoveGroups(List<string> groups, string siteUrl)
- {
- List<NameValuePair> groupstatus = new List<NameValuePair>();
-
- using (SPSite site = new SPSite(siteUrl))
- {
-
- using (SPWeb web = site.OpenWeb())
- {
- NameValuePair groupstat;
-
- foreach (string group in groups)
- {
- groupstat = new NameValuePair();
- try
- {
- web.Groups.Remove(group);
- groupstat.Name = string.Format("{0} removed sucessfully.", group);
- groupstat.Value = true;
- }
- catch (Exception ex)
- {
- groupstat.Name = string.Format("Error {1} while removing gorup {0}", group, ex.Message);
- groupstat.Value = false;
- }
-
- groupstatus.Add(groupstat);
- }
- }
- }
-
- return groupstatus;
- }
C# code to add user to web application.
- public bool AddUserToGroup(string siteUrl, string groupName, string memberName)
- {
- bool isOwnerUpdated = false;
- using (SPSite site = new SPSite(siteUrl))
- {
- using (SPWeb web = site.OpenWeb())
- {
-
-
- try
- {
- SPUser user = web.EnsureUser(memberName);
- SPGroup group = web.Groups[groupName];
- group.AddUser(user);
- group.Update();
- }
- catch (Exception ex)
- {
- }
- }
- }
-
- return isOwnerUpdated;
-
- }
C# code to set owner for web application.
- public bool SetOwnerToGroup(string siteUrl, string groupName, string ownerName, bool isGroup)
- {
- bool isOwnerUpdated = false;
-
- using (SPSite site = new SPSite(siteUrl))
- {
- using (SPWeb web = site.OpenWeb())
- {
- try
- {
- SPGroup group = web.Groups[groupName];
- SPMember owner;
- owner = web.Groups[ownerName];
- group.Owner = owner;
- group.Update();
- }
- catch (Exception ex)
- {
- isOwnerUpdated = false;
-
- }
- }
- }
- return isOwnerUpdated;
- }
C# code to get group members.
- public List<NameValuePair> GetGroupMembers(string siteUrl, string groupName)
- {
- List<NameValuePair> groupMembers = new List<NameValuePair>();
-
- using (SPSite site = new SPSite(siteUrl))
- {
-
- using (SPWeb web = site.OpenWeb())
- {
- NameValuePair groupMember;
-
- SPGroup group = web.Groups[groupName];
-
- foreach (SPUser user in group.Users)
- {
- groupMember = new NameValuePair();
- groupMember.Value = groupMember.Name = user.Name;
-
- groupMembers.Add(groupMember);
- }
- }
- }
-
- return groupMembers;
- }
C# code to check valid users.
- public bool IsValidUser(string UserName, SPWeb web)
- {
- SPAdministrationWebApplication centralAdmin = SPAdministrationWebApplication.Local;
-
- SPPrincipalInfo principle = SPUtility.ResolvePrincipal(centralAdmin, SPUrlZone.Default, UserName,
- SPPrincipalType.User, SPPrincipalSource.All, false);
-
-
- if (principle == null)
- return false;
- return true;
- }