1

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;
            }
        }
    }
});

2 Answers 2

2

Yes you can add All Authenticated Users or Domain Users.

This will provide permission to all Users in Active Directory.

// "c:0(.s|true" = All authenticated users
web.SiteGroups["Member"].AddUser("c:0(.s|true", string.Empty, 
 string.Empty, string.Empty);

Update

You can get Web's read permission group using following property

spWeb.AssociatedVisitorGroup
6
  • Alright but when creating a new teamsite my group gets a new name everytime the membersgroup plus my teamsites name for example "My members on This is my team site", next time the name of the teamsite is maybe something else how could i add users "all" to the group members without nowing the name of the members group?
    – Batistuta
    Commented Apr 20, 2015 at 12:24
  • How do u mean? So i should try to get the Groupname with the permission edit? how?
    – Batistuta
    Commented Apr 20, 2015 at 12:31
  • i have updated..
    – Batistuta
    Commented Apr 20, 2015 at 13:20
  • I will update my question, do u mean like that? @Amal
    – Batistuta
    Commented Apr 20, 2015 at 13:28
  • It really AssociatedVisitorGroup ( Permissionlevel (Edit) ?
    – Batistuta
    Commented Apr 20, 2015 at 13:48
0

I'm using the following script to add all Domain Users of my domain to the SharePoint Group "Members" for a given site. Note that Claims Authentication is used in this scenario.

function Add-DomainUsers([string]$SiteUrl)
{
    $spWeb = Get-SPWeb $SiteUrl
    $account = New-Object System.Security.Principal.NTAccount("MyDomain\Domain Users")
    $strSID = $account.Translate([System.Security.Principal.SecurityIdentifier])
    $spWeb.SiteGroups["Members"].AddUser("c:0+.w|$($strSID.Value)", "", "", "")
    $spWeb.Dispose()
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.