Script Repository


Copy SharePoint folder

October 14, 2015
1028

The script copies a SharePoint folder of a user. To copy SharePoint folders in Adaxes, you can add the script to a Business Rule, Custom Command or Scheduled Task using the Run a program or PowerShell script action.

Parameters:

  • $sharePointServer - specifies the NetBIOS name of the computer where the SharePoint Sever is homed;
  • $webApplicationURL - specifies the URL of the SharePoint web application;
  • $sourceFolderPath - specifies the path to the source folder;
  • $newFolderPath - specifies the path to the destination folder.
Note: You can use value references (e.g. %username%) to insert properties of the user account in the folder paths. For example, if you specify the following path Shared Documents/Folder A/%username% and execute the script on a user whose username is jdoe, the resulting folder path will be Shared Documents/Folder A/jdoe.
Edit Remove
PowerShell
$sharePointServer = "SharepointServer" # TODO: modify me

$webApplicationURL = "http://$sharePointServer/sites/MySite" # TODO: modify me
$sourceFolderPath = "Shared Documents/Folder A/%username%" # TODO: modify me
$newFolderPath = "Shared Documents/Folder B/%username%" # TODO: modify me

$session = New-PSSession $sharePointServer -Authentication Kerberos
$result = Invoke-Command -Session $session -ArgumentList $webApplicationURL, $sourceFolderPath, $newFolderPath -ScriptBlock {
    param($webApplicationURL, $sourceFolderPath, $newFolderPath)
    
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null
    
    $site = New-Object Microsoft.SharePoint.SPSite("$webApplicationURL")
    $web = $site.OpenWeb();
    
    $folder = $web.GetFolder($sourceFolderPath)
    if ($folder.Exists)
    {
        $folder.CopyTo($newFolderPath)
        return "Folder copied successfully" # TODO: modify me
    }
    else
    {
        return "The source folder is missing" # TODO: modify me
    }
}
Remove-PSSession -Session $session

$Context.LogMessage($result, "Information")

Comments ( 0 )
No results found.
Leave a comment