-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #590 from reshmee011/movefiles
New sample move files in a different library
- Loading branch information
Showing
3 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
--- | ||
plugin: add-to-gallery | ||
--- | ||
|
||
# Copying files between different SharePoint libraries with custom metadata | ||
|
||
You might have a requirement to move sample files from a site to a different site, e.g. subset of production files to UAT site to allow testing of solutions. You may want better control over metadata settings, such as ProcessStatus, ensuring files are marked as "Pending" upon transfer . Unlike the default file copy feature, this script enables you to skip the copy process if the destination site lacks a matching folder structure as well setting custom metadata to specific values. | ||
|
||
## Summary | ||
|
||
# [PnP PowerShell](#tab/pnpps) | ||
|
||
```PowerShell | ||
param ( | ||
[Parameter(Mandatory=$false)] | ||
[string]$SourceSiteUrl = "https://contoso.sharepoint.com/teams/app", | ||
[Parameter(Mandatory=$false)] | ||
[string]$SourceFolderPath= "https://contoso.sharepoint.com/teams/app/Temp Library/test", | ||
[Parameter(Mandatory=$false)] | ||
[string]$DestinationSiteUrl = "https://contoso.sharepoint.com/teams/t-app", | ||
[Parameter(Mandatory=$false)] | ||
[string]$DestinationFolderPath = "https://contoso.sharepoint.com/teams/t-app/TempLibrary/test" | ||
) | ||
# Generate a unique log file name using today's date | ||
$todayDate = Get-Date -Format "yyyy-MM-dd" | ||
$logFileName = "CopyFilesToSharePoint_$todayDate.log" | ||
$logFilePath = Join-Path -Path $PSScriptRoot -ChildPath $logFileName | ||
# Connect to the source and destination SharePoint sites | ||
Connect-PnPOnline -Url $SourceSiteUrl -Interactive | ||
$SourceConn = Get-PnPConnection | ||
Connect-PnPOnline -Url $DestinationSiteUrl -Interactive | ||
$DestConn = Get-PnPConnection | ||
# Function to copy files recursively and log errors | ||
function Copy-FilesToSharePoint { | ||
param ( | ||
[string]$SourceFolderPath, | ||
[string]$DestinationFolderPath | ||
) | ||
$sourceRelativeFolderPath = $SourceFolderPath.Replace($SourceSiteUrl,'') | ||
$sourceFiles = Get-PnPFolderItem -FolderSiteRelativeUrl $sourceRelativeFolderPath -ItemType File -Connection $SourceConn | ||
foreach ($file in $sourceFiles) { | ||
$relativePath = $file.ServerRelativePath | ||
# Check if the destination folder exists | ||
$destinationFolder = Get-PnPFolder -Url $DestinationFolderPath -Connection $DestConn -ErrorAction SilentlyContinue | ||
if ($null -eq $destinationFolder) { | ||
$errorMessage = "Error: Destination folder '$DestinationFolderPath' does not exist." | ||
Write-Host $errorMessage -ForegroundColor Red | ||
Add-Content -Path $logFilePath -Value $errorMessage | ||
continue | ||
} | ||
try { | ||
#get file as stream | ||
$fileUrl = $SourceFolderPath + "/" + $file.Name | ||
$p = $fileUrl.Replace($SourceSiteUrl,'') | ||
$streamResult = Get-PnPFile -Url $p -Connection $SourceConn -AsMemoryStream | ||
# Upload the file to the destination folder | ||
$uploadedFile = Add-PnPFile -Folder $DestinationFolderPath -FileName $file.Name -Stream $streamResult -Values @{"ProcessStatus" = "Pending"} -Connection $DestConn #-ErrorAction St | ||
Write-Host "File '$($file.Name)' copied and status set to 'Pending' in '$DestinationFolderPath'" -ForegroundColor Green | ||
} catch { | ||
$errorMessage = "Error copying file '$($file.Name)' to '$DestinationFolderPath': $($_.Exception.Message)" | ||
Write-Host $errorMessage -ForegroundColor Red | ||
Add-Content -Path $logFilePath -Value $errorMessage | ||
} | ||
} | ||
} | ||
# Call the function to copy files to SharePoint | ||
$sourceRelativeFolderPath = $SourceFolderPath.Replace($SourceSiteUrl,'') | ||
$sourceLevel1Folders = Get-PnPFolderItem -FolderSiteRelativeUrl $sourceRelativeFolderPath -ItemType Folder -Connection $SourceConn | ||
Copy-FilesToSharePoint -SourceFolderPath $SourceFolderPath -DestinationFolderPath $DestinationFolderPath | ||
$sourceLevel1Folders | ForEach-Object { | ||
$sourceLevel1Folder = $_ | ||
if($_.Name -ne "Forms"){ | ||
$sourcePath = $SourceFolderPath + "/" + $sourceLevel1Folder.Name | ||
$destPath = $DestinationFolderPath + "/" + $sourceLevel1Folder.Name | ||
Copy-FilesToSharePoint -SourceFolderPath $sourcePath -DestinationFolderPath $destPath | ||
} | ||
$sourceLevel1Path = $sourceRelativeFolderPath + "/" + $_.Name | ||
$sourceLevel2Folders = Get-PnPFolderItem -FolderSiteRelativeUrl $sourceLevel1Path -ItemType Folder -Connection $SourceConn | ||
$sourceLevel2Folders | ForEach-Object { | ||
$sourceLevel2Folder = $_ | ||
$sourcePath = $SourceFolderPath + "/" + $sourceLevel1Folder.Name + "/" + $sourceLevel2Folder.Name | ||
$destPath = $DestinationFolderPath + "/" + $sourceLevel1Folder.Name + "/" + $sourceLevel2Folder.Name | ||
Copy-FilesToSharePoint -SourceFolderPath $sourcePath -DestinationFolderPath $destPath | ||
} | ||
} | ||
# Disconnect from SharePoint | ||
``` | ||
[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)] | ||
*** | ||
|
||
## Contributors | ||
|
||
| Author(s) | | ||
|-----------| | ||
| Reshmee Auckloo | | ||
|
||
|
||
[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)] | ||
<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/spo-move-files-library-sites" aria-hidden="true" /> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
[ | ||
{ | ||
"name": "spo-move-files-library-sites", | ||
"source": "pnp", | ||
"title": "Copying files between different SharePoint libraries with custom metadata", | ||
"shortDescription": "This sample shows how to copy files between different SharePoint libraries with custom metadata with a matching folder structure.", | ||
"url": "https://pnp.github.io/script-samples/spo-move-files-library-sites/README.html", | ||
"longDescription": [ | ||
"This sample shows how to copy files between different SharePoint libraries with custom metadata with a matching folder structure." | ||
], | ||
"creationDateTime": "2023-10-01", | ||
"updateDateTime": "2023-10-01", | ||
"products": [ | ||
"SharePoint" | ||
], | ||
"metadata": [ | ||
{ | ||
"key": "PNP-POWERSHELL", | ||
"value": "2.1.1" | ||
} | ||
], | ||
"categories": [ | ||
"Configure", | ||
"Security" | ||
], | ||
"tags": [ | ||
"Connect-PnPOnline", | ||
"Get-PnPFolderItem", | ||
"Get-PnPFolder", | ||
"Get-PnPFile", | ||
"Add-PnPFile" | ||
], | ||
"thumbnails": [ | ||
{ | ||
"type": "image", | ||
"order": 100, | ||
"url": "https://raw.githubusercontent.com/pnp/script-samples/main/scripts/spo-move-files-library-sites/assets/preview.png", | ||
"alt": "preview image for the sample" | ||
} | ||
], | ||
"authors": [ | ||
{ | ||
"name": "Reshmee Auckloo", | ||
"gitHubAccount": "reshmee011", | ||
"company": "", | ||
"pictureUrl": "https://avatars.githubusercontent.com/u/7693852?v=4" | ||
} | ||
], | ||
"references": [ | ||
{ | ||
"name": "Want to learn more about PnP PowerShell and the cmdlets", | ||
"description": "Check out the PnP PowerShell site to get started and for the reference to the cmdlets.", | ||
"url": "https://aka.ms/pnp/powershell" | ||
} | ||
] | ||
} | ||
] |