The following code can be used to add user to a specific group.
Requirement - Business want to add a specific user to a site collection and all it's sub-site in a specific group. Ex: Full Control or Read or Edit.
using (ClientContext clientContextSub = new ClientContext(siteUrl))
{
SecureString passWord = new SecureString();
foreach (char c in password.ToCharArray()) passWord.AppendChar(c);
clientContextSub.Credentials = new SharePointOnlineCredentials(userName, passWord);
Web webSubWeb = clientContextSub.Web;
GroupCollection groupCollection = clientContextSub.Web.SiteGroups;
UserCollection ucol = clientContextSub.Web.SiteUsers;
clientContextSub.Load(webSubWeb);
clientContextSub.ExecuteQuery();
//txtAddUserToGroup is a text file to add to groups
Principal user = ucol.GetByEmail(txtAddUserToGroup.Text.ToString().Trim());
clientContextSub.Load(user);
clientContextSub.ExecuteQuery();
// dtGroupUsers is a datatable logging who's being added to which group with other information
if (dtGroupandUsers.Rows.Count == 0)
{
if(dtGroupandUsers.Columns.Count==0)
{
dtGroupandUsers.Columns.Add(constantVariables.SiteURL);
dtGroupandUsers.Columns.Add(constantVariables.Title);
dtGroupandUsers.Columns.Add(constantVariables.userGroups);
dtGroupandUsers.Columns.Add(constantVariables.userLogin);
dtGroupandUsers.Columns.Add(constantVariables.emailAddress);
dtGroupandUsers.Columns.Add(constantVariables.name);
dtGroupandUsers.Columns.Add(constantVariables.LastModified);
dtGroupandUsers.Columns.Add(constantVariables.siteInheritance);
dtGroupandUsers.Columns.Add(constantVariables.groupOwner);
dtGroupandUsers.Columns.Add(constantVariables.permission);
}
}
else
{ dtGroupandUsers.Rows.Clear(); }
if (user != null)
{
addUsertoGroup(webSubWeb, clientContextSub,user);
}
}
}
///***Other Function ***///
public void addUsertoGroup(Web webSubWeb, ClientContext clientContextSub, Principal user)
{
string groupOwener = string.Empty;
clientContextSub.Load(webSubWeb);
clientContextSub.Load(webSubWeb.Webs);
clientContextSub.Load(webSubWeb.RoleAssignments);
clientContextSub.ExecuteQuery();
foreach (RoleAssignment assignment in webSubWeb.RoleAssignments)
{
string Role = string.Empty;
clientContextSub.Load(assignment.Member);
clientContextSub.Load(assignment.RoleDefinitionBindings);
GroupCollection groupCollection = webSubWeb.SiteGroups;
clientContextSub.ExecuteQuery();
foreach (RoleDefinition roleDef in assignment.RoleDefinitionBindings)
{
if (roleDef.Name != string.Empty)
{
//cmbUserCollection.Text is a dropdown control and user has selected "Full Control" and any group having permission "Full Control" with add this user.
if (roleDef.Name.ToString().ToLower() == cmbUserCollection.Text.ToString().ToLower())
{
int Cnt = assignment.RoleDefinitionBindings.Count;
Principal Pri = assignment.Member;
if (Pri.PrincipalType == PrincipalType.SharePointGroup)
{
UserCreationInformation userCreationInfo = new UserCreationInformation();
userCreationInfo.Email = user.LoginName;
userCreationInfo.LoginName = user.LoginName;
userCreationInfo.Title = user.Title;
Group grp = groupCollection.GetByName(Pri.LoginName);
clientContextSub.Load(grp);
clientContextSub.ExecuteQuery();
grp.Users.Add(userCreationInfo);
DataRow dtGU = dtGroupandUsers.NewRow();
dtGroupandUsers.Rows.Add(dtGU);
dtGU[0] = "\"" + webSubWeb.Url.ToString() + "\"";
dtGU[1] = "\"" + webSubWeb.Title.ToString() + "\"";
dtGU[2] = "\"" + grp.Title.ToString() + "\"";
dtGU[3] = "\"" + user.LoginName.ToString() + "\"";
if (user.LoginName.Contains('#'))
{
dtGU[4] = "\"" + user.LoginName.Substring(18, (user.LoginName.Length - 18)).Trim() + "\""; }
dtGU[5] = "\"" + user.Title.ToString() + "\"";
dtGU[6] = "\"" + webSubWeb.LastItemModifiedDate;
dtGU[7] = "";
dtGU[8] = "\"" + grp.OwnerTitle.ToString() + "\"";
dtGU[9] = roleDef.Name.ToString();
}
}
}
}
}
if (webSubWeb.Webs.Count > 0)
{
foreach (Web web in webSubWeb.Webs)
{
addUsertoGroup(web, clientContextSub, user);
}
}}
Requirement - Business want to add a specific user to a site collection and all it's sub-site in a specific group. Ex: Full Control or Read or Edit.
using (ClientContext clientContextSub = new ClientContext(siteUrl))
{
SecureString passWord = new SecureString();
foreach (char c in password.ToCharArray()) passWord.AppendChar(c);
clientContextSub.Credentials = new SharePointOnlineCredentials(userName, passWord);
Web webSubWeb = clientContextSub.Web;
GroupCollection groupCollection = clientContextSub.Web.SiteGroups;
UserCollection ucol = clientContextSub.Web.SiteUsers;
clientContextSub.Load(webSubWeb);
clientContextSub.ExecuteQuery();
//txtAddUserToGroup is a text file to add to groups
Principal user = ucol.GetByEmail(txtAddUserToGroup.Text.ToString().Trim());
clientContextSub.Load(user);
clientContextSub.ExecuteQuery();
// dtGroupUsers is a datatable logging who's being added to which group with other information
if (dtGroupandUsers.Rows.Count == 0)
{
if(dtGroupandUsers.Columns.Count==0)
{
dtGroupandUsers.Columns.Add(constantVariables.SiteURL);
dtGroupandUsers.Columns.Add(constantVariables.Title);
dtGroupandUsers.Columns.Add(constantVariables.userGroups);
dtGroupandUsers.Columns.Add(constantVariables.userLogin);
dtGroupandUsers.Columns.Add(constantVariables.emailAddress);
dtGroupandUsers.Columns.Add(constantVariables.name);
dtGroupandUsers.Columns.Add(constantVariables.LastModified);
dtGroupandUsers.Columns.Add(constantVariables.siteInheritance);
dtGroupandUsers.Columns.Add(constantVariables.groupOwner);
dtGroupandUsers.Columns.Add(constantVariables.permission);
}
}
else
{ dtGroupandUsers.Rows.Clear(); }
if (user != null)
{
addUsertoGroup(webSubWeb, clientContextSub,user);
}
}
}
///***Other Function ***///
public void addUsertoGroup(Web webSubWeb, ClientContext clientContextSub, Principal user)
{
string groupOwener = string.Empty;
clientContextSub.Load(webSubWeb);
clientContextSub.Load(webSubWeb.Webs);
clientContextSub.Load(webSubWeb.RoleAssignments);
clientContextSub.ExecuteQuery();
foreach (RoleAssignment assignment in webSubWeb.RoleAssignments)
{
string Role = string.Empty;
clientContextSub.Load(assignment.Member);
clientContextSub.Load(assignment.RoleDefinitionBindings);
GroupCollection groupCollection = webSubWeb.SiteGroups;
clientContextSub.ExecuteQuery();
foreach (RoleDefinition roleDef in assignment.RoleDefinitionBindings)
{
if (roleDef.Name != string.Empty)
{
//cmbUserCollection.Text is a dropdown control and user has selected "Full Control" and any group having permission "Full Control" with add this user.
if (roleDef.Name.ToString().ToLower() == cmbUserCollection.Text.ToString().ToLower())
{
int Cnt = assignment.RoleDefinitionBindings.Count;
Principal Pri = assignment.Member;
if (Pri.PrincipalType == PrincipalType.SharePointGroup)
{
UserCreationInformation userCreationInfo = new UserCreationInformation();
userCreationInfo.Email = user.LoginName;
userCreationInfo.LoginName = user.LoginName;
userCreationInfo.Title = user.Title;
Group grp = groupCollection.GetByName(Pri.LoginName);
clientContextSub.Load(grp);
clientContextSub.ExecuteQuery();
grp.Users.Add(userCreationInfo);
DataRow dtGU = dtGroupandUsers.NewRow();
dtGroupandUsers.Rows.Add(dtGU);
dtGU[0] = "\"" + webSubWeb.Url.ToString() + "\"";
dtGU[1] = "\"" + webSubWeb.Title.ToString() + "\"";
dtGU[2] = "\"" + grp.Title.ToString() + "\"";
dtGU[3] = "\"" + user.LoginName.ToString() + "\"";
if (user.LoginName.Contains('#'))
{
dtGU[4] = "\"" + user.LoginName.Substring(18, (user.LoginName.Length - 18)).Trim() + "\""; }
dtGU[5] = "\"" + user.Title.ToString() + "\"";
dtGU[6] = "\"" + webSubWeb.LastItemModifiedDate;
dtGU[7] = "";
dtGU[8] = "\"" + grp.OwnerTitle.ToString() + "\"";
dtGU[9] = roleDef.Name.ToString();
}
}
}
}
}
if (webSubWeb.Webs.Count > 0)
{
foreach (Web web in webSubWeb.Webs)
{
addUsertoGroup(web, clientContextSub, user);
}
}}