8

I've found a script to create document libraries and a script to assign permissions to a list. I've combined these with one script and added a CSV containing names and Office 365 Groups that should be added to the list.

The purpose of the script is to execute the following tasks:

  1. Read the CSV file line by line (Foreach)
  2. Set DocLib Name and Off365Group
  3. Create library with DocLib as name and description
  4. Add Domain admins (Office 365 Group) as owner to the list
  5. Add CustomerLocation-DocLib (Office 365 Group) as contributor to the list

Part 1 - 3 are working As for part 4/5, I think that the script is currently polling the SharePoint Groups of the site, not Office Groups.

My question is, how can I extend the script to use Office Groups. The user management will be done through the Admin Panel of Office 365 by the customer.

Thanks!

PS: I take no credit for the script because it is created with samples found trough Google :-)

// Script

$siteURL = "https://contoso.sharepoint.com/sites/test"
$userName = "admin@contoso.sharepoint.com"
$Password = "SuperSecretPassword"

# ----------------------------------------------------------------------------

$ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition

Add-Type -Path "C:\Git\office-365\Losse Apps\365App\lib\SharePointOnline\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Git\office-365\Losse Apps\365App\lib\SharePointOnline\Microsoft.SharePoint.Client.Runtime.dll"

function Add-SPPermissionToListGroup
{
    param (
        [Parameter(Mandatory=$true)][string]$Url,
        [Parameter(Mandatory=$true)][string]$ListName,
        [Parameter(Mandatory=$true)][string]$GroupName,
        [Parameter(Mandatory=$true)][string]$PermissionLevel
    )

    #$web = Get-SPOWeb -Identity $Url

    $list = $Context.web.Lists.GetByTitle($ListName)

    if ($list -ne $null)
    {
        # Ensure that the permissions are not being inherited.
        if ($list.HasUniqueRoleAssignments -eq $False)
        {
            $list.BreakRoleInheritance($True)
        }

        # Modify the permissions.
        if ($Context.web.SiteGroups[$GroupName] -ne $null)
        {
            $group = $Context.web.SiteGroups[$GroupName]
            $roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)
            $roleDefinition = $Context.web.RoleDefinitions[$PermissionLevel];
            $roleAssignment.RoleDefinitionBindings.Add($roleDefinition);
            $list.RoleAssignments.Add($roleAssignment)
            $list.Update();
            Write-Host "Successfully added $PermissionLevel permission to $GroupName group in $ListName list. " -foregroundcolor Green
        }
        else
        {
            Write-Host "Group $GroupName does not exist." -foregroundcolor Red
        }

    }
}

Function CreateDocumentLibrary {
   [CmdletBinding()]

    Param (
        [Parameter(Mandatory=$true)][string]$Name
    )

    $listTitle = $Name
    $listDescription = $Name
    $listTemplate = 101

    # Creating client context object
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
    $context.credentials = $SPOCred

    #create list using ListCreationInformation object (lci)
    $lci = New-Object Microsoft.SharePoint.Client.ListCreationInformation
    $lci.title = $listTitle
    $lci.description = $listDescription
    $lci.TemplateType = $listTemplate
    $list = $context.web.lists.add($lci)
    $context.load($list)

    #send the request containing all operations to the server
    try {
        $context.executeQuery()
        write-host "info: Created $($listTitle)" -foregroundcolor green
    }

    catch {
        write-host "info: $($_.Exception.Message)" -foregroundcolor red
    }
}

$SPOPass = ConvertTo-SecureString $Password -AsPlainText -Force
$SPOCred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $SPOPass)

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
$Context.credentials = $SPOCred

Import-CSV $ScriptPath\DocumentLibraries.csv | ForEach-Object {
    $DocLib = $_.DocumentLibrary
    $Group = $_.Group

    #CreateDocumentLibrary -Name $DocLib
    Add-SPPermissionToListGroup -Url $siteURL -ListName $DocLib -GroupName 'Domain Admins' -PermissionLevel 'Full Control'
    Add-SPPermissionToListGroup -Url $siteURL -ListName $DocLib -GroupName $Group -PermissionLevel Contribute

}

$Context.Dispose()

// CVS

DocumentLibrary,Group
Test1, CustomerLocation-Doc-Test1
Test2, CustomerLocation-Doc-Test2
Test3, CustomerLocation-Doc-Test3
Test4, CustomerLocation-Doc-Test4
Test5, CustomerLocation-Doc-Test5
  • What do you mean by Office 365 Groups? Security Groups or Unified Groups? If Unified Groups then unfortunately no - it is not possible. – grisha Sep 21 '16 at 11:10
  • No I meant security groups – Paul_dH Oct 23 '16 at 15:21
2

Check out the Office Dev PNP for PowerShell. https://github.com/SharePoint/PnP-PowerShell. You can get this done in a few lines vs all that custom CSOM.

Connect-PnpOnline https://tenant.sharepoint.com
New-PnPList -Title NameMe -Template Library
New-PnPGroup -Title GroupName
Set-PnPGroupPermissions -Identity GroupName -List NameMe -AddRole Contribute

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.