0

I'm trying to get the following code (originally taken from https://gallery.technet.microsoft.com/office/SharePoint-online-57f24eca) to read through a CSV file where one of the columns has two user groups. I've tried using -split to break down the contents but I'm not having any luck and keep getting "Group cannot be found" Is anyone able to help me please?

CSV example:

Folder,Group,Role
RenameMe,"Group1,Group2",Edit
RenameMe/TestFolder1,Group1,Edit

Code:

#Get the user credentials
$username = "username"
$securePassword = ConvertTo-SecureString "password" -AsPlainText -Force

#Input Parameters
$url = "https://website.sharepoint.com/sites/site1"
$csvfilepath ='C:\temp\data.csv'
$libname ='Shared Documents/Folder1'

#References
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'


#CreateFolder with Permissions Function
function CreateFolderWithPermissions()
{

    #Connect to SharePoint Online and get ClientContext object.
    $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
    $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
    $clientContext.Credentials = $credentials


    Function GetRole
    {
        [CmdletBinding()]
        param
        (
            [Parameter(Mandatory = $true, Position = 1)]
            [Microsoft.SharePoint.Client.RoleType]$rType
        )

        $web = $clientContext.Web
        if ($web -ne $null)
        {
            $roleDefs = $web.RoleDefinitions
            $clientContext.Load($roleDefs)
            $clientContext.ExecuteQuery()
            $roleDef = $roleDefs | Where-Object { $_.RoleTypeKind -eq $rType }
            return $roleDef
        }
        return $null
    }

    #Get the SharePoint web
    $web = $clientContext.Web;
    $clientContext.Load($web)

    #Get the groups
    $groups = $web.SiteGroups
    $clientContext.Load($groups)
    $clientContext.ExecuteQuery()


    #Read CSV File and iterate
    $csv = Import-CSV $csvfilepath
    foreach ($row in $csv)
    {

        #Create Folder
        $folder = $web.Folders.Add($libname + '/' + $row.Folder)
        $clientContext.Load($folder)
        $clientContext.ExecuteQuery()

        #Assign Role
        $group = $groups.GetByName($row.Group)
        $clientContext.Load($group)
        $clientContext.ExecuteQuery()

        $roleType= $row.Role
        $roleTypeObject = [Microsoft.SharePoint.Client.RoleType]$roleType
        $roleObj = GetRole $roleTypeObject
        $usrRDBC = $null
        $usrRDBC = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($clientContext)
        $usrRDBC.Add($roleObj)

         #Remove inherited permissions
        $folder.ListItemAllFields.BreakRoleInheritance($false, $true)
        $clientContext.Load($folder.ListItemAllFields.RoleAssignments.Add($group, $usrRDBC))
        $folder.Update()
        $clientContext.ExecuteQuery()

        #Display the folder name and permission
        Write-Host -ForegroundColor Green 'Folder Name: ' $folder.Name ' Group: '$row.Group ' Role: ' $roleType;

    }

}
#Execute the function
CreateFolderWithPermissions

1 Answer 1

0

I don't know exactly what split method you tried, but executing $row.Group.Split(",") works fine on the two sample rows your provided, including the case where there is only one group in the field. Don't forget that after you .Split(","), you then have to loop through your split result. The following should work:

#Get the user credentials
$username = "username"
$securePassword = ConvertTo-SecureString "password" -AsPlainText -Force

#Input Parameters
$url = "https://website.sharepoint.com/sites/site1"
$csvfilepath ='C:\temp\data.csv'
$libname ='Shared Documents/Folder1'

#References
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'


#CreateFolder with Permissions Function
function CreateFolderWithPermissions()
{

    #Connect to SharePoint Online and get ClientContext object.
    $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
    $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
    $clientContext.Credentials = $credentials


    Function GetRole
    {
        [CmdletBinding()]
        param
        (
            [Parameter(Mandatory = $true, Position = 1)]
            [Microsoft.SharePoint.Client.RoleType]$rType
        )

        $web = $clientContext.Web
        if ($web -ne $null)
        {
            $roleDefs = $web.RoleDefinitions
            $clientContext.Load($roleDefs)
            $clientContext.ExecuteQuery()
            $roleDef = $roleDefs | Where-Object { $_.RoleTypeKind -eq $rType }
            return $roleDef
        }
        return $null
    }

    #Get the SharePoint web
    $web = $clientContext.Web;
    $clientContext.Load($web)

    #Get the groups
    $groups = $web.SiteGroups
    $clientContext.Load($groups)
    $clientContext.ExecuteQuery()


    #Read CSV File and iterate
    $csv = Import-CSV $csvfilepath
    foreach ($row in $csv)
    {
        #split groups from csv
        $rowgroups = $row.group.split(",");

        foreach ($rowGroup in $rowgroups)
        {
            #Create Folder
            $folder = $web.Folders.Add($libname + '/' + $row.Folder)
            $clientContext.Load($folder)
            $clientContext.ExecuteQuery()

            #Assign Role
            $group = $groups.GetByName($rowGroup)
            $clientContext.Load($group)
            $clientContext.ExecuteQuery()

            $roleType= $row.Role
            $roleTypeObject = [Microsoft.SharePoint.Client.RoleType]$roleType
            $roleObj = GetRole $roleTypeObject
            $usrRDBC = $null
            $usrRDBC = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($clientContext)
            $usrRDBC.Add($roleObj)

             #Remove inherited permissions
            $folder.ListItemAllFields.BreakRoleInheritance($false, $true)
            $clientContext.Load($folder.ListItemAllFields.RoleAssignments.Add($group, $usrRDBC))
            $folder.Update()
            $clientContext.ExecuteQuery()

            #Display the folder name and permission
            Write-Host -ForegroundColor Green 'Folder Name: ' $folder.Name ' Group: '$rowGroup ' Role: ' $roleType;
        }
    }

}
#Execute the function
CreateFolderWithPermissions
1
  • Thank you so much, I tried something similar but I was missing ';' at the end of the split line and struggling to get my head around where to insert the 'foreach' So simple when you know how.
    – Phil N
    Commented Feb 28, 2020 at 13:25

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.