Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Please help me about adding new AD users, AD groups with Permission to sharepoint site. I can do it for lists and document libararys but adding to site seems to be different. This is what i do for lists:

Function Rights ($user, $ShortListName, $permission){

$userName = $spweb.EnsureUser($User)
$spFolder = $spweb.lists[$ShortListName]
if($spweb.SiteUsers[$userName])
    {
        $assignment=new-object  
Microsoft.SharePoint.SPRoleAssignment($spweb.SiteUsers[$userName])
        $assignment.RoleDefinitionBindings.Add($spweb.RoleDefinitions[$permission])
        $spfolder.BreakRoleInheritance($false, $false)
        $spfolder.RoleAssignments.add($assignment);
        Write-Host "Permission provided for group ", $userName
    $spfolder.update()
    }
 else{
      Write-Host "group ""$userName"" was not found in this web!"
    }
}

If i simply change the spfolder spweb, it won't work

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

If you want to add permission to the SPWeb, try this:

$account = $web.EnsureUser($userName)
$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($account)
$role = $web.RoleDefinitions[$permission]
$assignment.RoleDefinitionBindings.Add($role)
$web.RoleAssignments.Add($assignment)
$web.dispose()
share|improve this answer
add comment

It's not clear to me what you are asking for. The title of your question is regarding adding an "AD user to SharePoint site" but your script is setting specific permissions on a folder and granting a user/group those permissions. Are you just trying to add a user or AD group to a SharePoint group like the owners/members/visitors groups? If that is the case, use this:

Set-SPUser -Identity 'domain\UserName' -Web 'http://sharepointdomain.com/sites/web' -Group 'Site Members'

share|improve this answer
1  
No. I wanted to add AD users and AD groups with permission to sp site. My code is for adding to lists. –  Lokids Aug 27 '13 at 14:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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