Can I add "All" (Users) to SharePoint group members?
string userGroupName = "Member";
SPSecurity.RunWithElevatedPrivileges(delegate
{
//Don't use context to create the spSite object since it won't create the object with elevated privileges but with the privileges of the user who execute the this code, which may casues an exception
using (SPSite spSite = SPContext.Current.Web.Site)
{
using (SPWeb spWeb = spSite.OpenWeb())
{
try
{
//Allow updating of some sharepoint lists, (here spUsers, spGroups etc...)
spWeb.AllowUnsafeUpdates = true;
SPUser allusers = spWeb.EnsureUser("NT AUTHORITY\\authenticated users");
if (allusers != null)
{
SPGroup spGroup = spWeb.Groups[userGroupName];
if (spGroup != null)
spGroup.AddUser(allusers);
}
}
catch (Exception ex)
{
//Error handling logic should go here
}
finally
{
spWeb.AllowUnsafeUpdates = false;
}
}
}
}
UPDATE 15:28
string usersAll = "NT AUTHORITY\authenticated users";
SPSecurity.RunWithElevatedPrivileges(delegate
{
//Don't use context to create the spSite object since it won't create the object with elevated privileges but with the privileges of the user who execute the this code, which may casues an exception
using (SPSite spSite = SPContext.Current.Web.Site)
{
using (SPWeb spWeb = spSite.OpenWeb())
{
try
{
spWeb.AllowUnsafeUpdates = true;
SPUser allusers = spWeb.EnsureUser(usersAll);
if (allusers != null)
{
SPGroup spGroup = spWeb.AssociatedVisitorGroup;
if (spGroup != null)
spGroup.AddUser(allusers);
}
}
catch (Exception)
{
//Error handling logic should go here
}
finally
{
spWeb.AllowUnsafeUpdates = false;
}
}
}
});