1

I have the script below to create a group on a site collection in sharepoint online.

I would like to know how can I change it in order to create several groups in the same site collection?

Many thanks in advance

$host.Runspace.ThreadOptions = "ReuseThread" 

#Definition of the function that allows to create a SharePoint Group in a SharePoint Online Site function 

Create-SPOGroup { param ($sSiteColUrl,$sUsername,$sPassword,$sGroupToCreate,$sGroupToCreateDescription) try { 

Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green 

Write-Host "Creating SharePoint Group $sGroupToCreate in $sSiteColUrl" -foregroundcolor Green 

Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green 

#Adding the Client OM Assemblies 

Add-Type -Path "C:\CSOMDLL\Microsoft.SharePoint.Client.dll" 

Add-Type -Path "C:\CSOMDLL\Microsoft.SharePoint.Client.Runtime.dll" 

#SPO Client Object Model Context 

$spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteColUrl) $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUsername, $sPassword) $spoCtx.Credentials = $spoCredentials 

#Root Web Site 

$spoRootWebSite = $spoCtx.Web #Object for creating a new SPO Group $spoGroupCreationInfo=New-Object Microsoft.SharePoint.Client.GroupCreationInformation $spoGroupCreationInfo.Title=$sGroupToCreate $spoGroup=$spoRootWebSite.SiteGroups.Add($spoGroupCreationInfo) $spoCtx.ExecuteQuery() 

Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green 

Write-Host "SharePoint Group $sGroupToCreate in $sSiteColUrl created succesfully!!" -foregroundcolor Green 

Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green $spoCtx.Dispose() } catch [System.Exception] { 

write-host -f red $_.Exception.ToString() } } 

#Required Parameters 
$sSiteColUrl = "sitecol" 
$sUsername = "user" 

#$sPassword = Read-Host -Prompt "pass" -AsSecureString 
$sPassword=convertto-securestring "pass" -asplaintext -force $sGroupToCreate="Sharepointgroup3" 

Create-SPOGroup -sSiteColUrl $sSiteColUrl -sUsername $sUsername -sPassword $sPassword -sGroupToCreate $sGroupToCreate
0

Below piece of code will help you, but it is in CSOM.

 public string CreateGroup(ClientContext clientContext, string title, string description)
    {

        clientContext.RequestTimeout = -1;
        string strGroupNameAlrdyExst = string.Empty;
        try
        {
            //  Microsoft.SharePoint.Client.Group sgroup = GetGroupDetail(title, slogin);
            Site site = clientContext.Site;
            Web web = clientContext.Web;
            GroupCollection grupcoll = web.SiteGroups;

            clientContext.Load(grupcoll, grups => grups.Include(grup => grup.Title).Where(grup => grup.Title == title));
            clientContext.ExecuteQuery();

            //create the group
            if (grupcoll.Count > 0)
            {
                strGroupNameAlrdyExst = title;
            }
            else
            {                  
                GroupCreationInformation grp = new GroupCreationInformation();
                grp.Title = title;
                grp.Description = description;

                //add it to the list of site groups
                Microsoft.SharePoint.Client.Group newgrp = clientContext.Web.SiteGroups.Add(grp);
                clientContext.ExecuteQuery();
            }
            return strGroupNameAlrdyExst;
        }
        catch (Exception ex)
        {

            return strGroupNameAlrdyExst;
        }

    }

Run it in for loop for creating multiple.

  • I need it in CSOM.. could you please guys help? – Peter Smp May 31 '17 at 14:50
  • Yeah it is in CSOM – Anil Pal May 31 '17 at 14:50
  • check updated code – Anil Pal May 31 '17 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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