Saturday, April 2, 2016

SharePoint Online: How to Create Group using PowerShell CSOM

A SharePoint group is a container of users who share similar set of permissions. SharePoint groups provides convenient way to manage security as a group centrally rather managing individual users. We can organize users into groups (up to5,000) and set permissions to the group at site level and then use it in any underlying objects like sub-sites, lists and list items.

By default SharePoint creates three user groups upon site creation: Owners, Members and visitors and you need to be a site owner/site collection admin to grant permissions to the group.

How to Create a group in SharePoint Online:

  • Navigate to your SharePoint online site, Click on Site Settings gear icon and then select Site Settings. From the Site settings page, click Site Permissions in the Users And Permissions section.
  • From the Permissions page, click on Create Group icon from the ribbon, under Permissions tab.
    SharePoint Online PowerShell to create group
  • On the Create Group page, provide the Name, optionally other settings such as description, owner to the group. Set group settings like who can view/edit group membership, Allow users to request membership,  leave the group. All requests will be sent to the specified E-mail.
  • In the "Give group permission to this site" section, select the appropriate check box to assign the permission level to your new group.
    SharePoint online Create Group using PowerShell
  • Click Create.

Once the group is created, you can rename it, change permissions of the group in one place and add/remove people to it. Lets see the PowerShell for SharePoint online to create group.

SharePoint Online PowerShell to create group
Lets create a new user group in SharePoint online using PowerShell.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#Variables for Processing
$SiteURL = "https://Abraaj.sharepoint.com/Sites/Sales"
$GroupName="Sales Managers90"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred

#Create new Group
$GroupInfo = New-Object Microsoft.SharePoint.Client.GroupCreationInformation
$GroupInfo.Title = $GroupName      
$Group = $Ctx.web.SiteGroups.Add($GroupInfo)
$Ctx.ExecuteQuery()
Plain and simple, huh.. Lets add some error handling code and assign permissions to the created group.

SharePoint online Create Group using PowerShell
Here is the PowerShell CSOM script to create SharePoint group.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#Variables for Processing
$SiteURL = "https://crescent.sharepoint.com/Sites/Sales"
$GroupName="Sales Managers"
$PermissionLevel="Edit"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred

    #Get all existing groups of the site
    $Groups = $Ctx.Web.SiteGroups
    $Ctx.load($Groups)
    $Ctx.ExecuteQuery()
    
    #Get Group Names
    $GroupNames =  $Groups | Select -ExpandProperty Title
    
    #Check if the given group doesn't exist already
    If($GroupNames -notcontains $GroupName)
    {
        #Create new Group
        $GroupInfo = New-Object Microsoft.SharePoint.Client.GroupCreationInformation
        $GroupInfo.Title = $GroupName      
        $Group = $Ctx.web.SiteGroups.Add($GroupInfo)
        $Ctx.ExecuteQuery()

        #Assign permission to the group
        $RoleDef = $Ctx.web.RoleDefinitions.GetByName($PermissionLevel)
        $RoleDefBind = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($Ctx)
        $RoleDefBind.Add($RoleDef)
        $Ctx.Load($Ctx.Web.RoleAssignments.Add($Group,$RoleDefBind))
        $Ctx.ExecuteQuery()

        write-host  -f Green "User Group has been added Successfully!"
    }
    else
    {
        Write-host -f Yellow "Group Exists already!"
    }
}
Catch {
    write-host -f Red "Error Creating New user Group!" $_.Exception.Message
}
This script creates new SharePoint online group and assigns permission to the group.



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...