0

Is there a way of amending this script here:

https://sharepoint.stackexchange.com/a/263621/89177

to be able to copy/create and apply perms to subfolders? (E.g: https://micrososft.sharepoint.com/sites/hgs_demo -> LibraryName -> Template Folder -> SubFolder 1 etc?

I've tried amending the variables but it doesn't seem to be able to 'find' subfolders.

TIA Phil

2
  • The script you are linking to already copies all subfolders.
    – willman
    Feb 20, 2020 at 20:44
  • Hi Willman, it only works for folders on the root of the document library, I would like to be able to duplicate a folder (with perms) underneath this.
    – Phil N
    Feb 20, 2020 at 20:48

1 Answer 1

0

Use the entire script as written, except for the section #### Parmeters and start script ##, then use the below:

##################################
#### Parmeters and start script ## 
##################################

$admin=""
$pass= ConvertTo-SecureString "" -AsPlainText -Force
$SiteURL="https://micrososft.sharepoint.com/sites/hgs_demo/"
$LibraryName="FolderTest";
$SourceFolderServerRelativeUrl="/sites/hgs_demo/libname/subfolder1"
$DestinationFolderServerRelativeUrl="/sites/hgs_demo/libname/folder0/destFolder1"
$global:ctx
Try 
{
    Connect-SPO -Username $admin -Url $SiteURL -AdminPassword $pass
    $Library=$ctx.Web.Lists.GetByTitle($LibraryName);
    $SrcFolder = $ctx.Web.GetFolderByServerRelativeUrl($SrcFolderServerRelativePath);
    $ctx.Load($Library);
    $ctx.Load($SrcFolder)
    $ctx.ExecuteQuery()   

    #CheckIfFolderExists
    $CheckRootFolderExists= Check-SPOFolderExists -FolderRelativeURL $DestinationFolderServerRelativeUrl
    if( $CheckRootFolderExists -eq "True")
    {
        Write-Host "A folder "$DestinationFolderServerRelativeUrl "already Exists" -ForegroundColor Green
    }
    else
    {
        #Calling Recursive Function with
        Recurse $SrcFolder
    }
}
catch
{

    Write-Host  $_.Exception.Message

} 


5
  • Thank you for your help! However I've just tried it and it's throwing an 'You cannot call a method on a null-valued expression' error
    – Phil N
    Feb 20, 2020 at 22:01
  • sorry, just noticed there was a typo $ctw instead of $ctx
    – willman
    Feb 20, 2020 at 22:03
  • Thanks willman. Just been having a play around in our environment and it keeps throwing the following no matter how I tweak the library, source or destination paths: Folder Doesn't Exist! Folder Name ->'' Number of List Items->'0' Copy this folder as a none root folder!! /sites/hgs_demo/ Add===> /sites/hgs_demo/ Exception calling "ExecuteQuery" with "0" argument(s): "File Not Found."
    – Phil N
    Feb 20, 2020 at 22:32
  • You have to update these variables to match your folder and library names: $LibraryName= $SourceFolderServerRelativeUrl= $DestinationFolderServerRelativeUrl=
    – willman
    Feb 20, 2020 at 23:06
  • Yeah done that but that's the error it throws. I think it relates to something within the ##Create New SubFolder section of the code
    – Phil N
    Feb 21, 2020 at 8:20

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.