0

I have a document libray with structure like given below: FolderA->Subfolder1->item1 Subfolder2-->item2 They have folder level permissions . now i want to create the same folder structure and permissions with different Folder Parent name for example:

Once we copy the folder structure should be FOLDER B-->Subfolder1->item1 Subfolder2-->item2

This is for sharepointonline CSOM powershell.

I was able to copy the folder structure but unable to get permissions is anything missing

    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
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.Taxonomy.dll"
#variables
$SiteURL="https://santosh.sharepoint.com/sites/Test_demo/"
$LibraryName="FolderTest";
$admin = ''
$password = ConvertTo-SecureString "sdsd" -AsPlainText -Force
$SrcFolderName="LibraryA"
$NewFolderName="sandy_Test"

Function Copy-SPOFolder([String]$SiteURL, [String]$SourceFolderURL, [String]$TargetFolderURL)
{
    Try{
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)


        #Copy the Folder
        $MoveCopyOpt = New-Object Microsoft.SharePoint.Client.MoveCopyOptions
        [Microsoft.SharePoint.Client.MoveCopyUtil]::CopyFolder($Ctx, $SourceFolderURL, $TargetFolderURL, $MoveCopyOpt)
        $Ctx.ExecuteQuery()

        Write-host -f Green "Folder Copied Successfully!"
    }
    Catch {
    write-host -f Red "Error Copying the Folder!" $_.Exception.Message
    }
}

Try {

        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($admin,$password)
        $Ctx.Credentials = $credentials
        $Web = $Ctx.web
        $Library=$Web.Lists.GetByTitle($LibraryName);
         $Folders = $Library.RootFolder.Folders
        $Ctx.Load($Library);
        $Ctx.Load($Folders)
        $Ctx.ExecuteQuery()        
   #Get existing folder names
        $FolderNames = $Folders | Select -ExpandProperty Name
        if($FolderNames -contains $NewFolderName)
        {
            write-host "Folder Exists Already!" -ForegroundColor Yellow
        }

        else{

                   $SourceFolderURL= $SiteURL + $LibraryName +"/" + $SrcFolderName
                    $TargetFolderURL= $SiteURL + $LibraryName +"/" + $NewFolderName 

                    Copy-SPOFolder $SiteURL $SourceFolderURL $TargetFolderURL

            }

    }

Catch {
        write-host -f Red "Error Granting permission to  Folder!" $_.Exception.Message
      }

Your Answer

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

Browse other questions tagged or ask your own question.