最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

powershell - The Copy-PnPFile method does not copy items from the document library to the SharePoint list - Stack Overflow

matteradmin8PV0评论

Hello everyone I have tried copy documents in document library, but have mistake: "Access denied". In code bellow I tried copy one element by id:

$clientId = "**************************"
$clientSecret = "**************************"

$siteUrl = "https://***********.sharepoint/sites/TransportClaimsDatabase/"
$SourceLibrary = "Claims (test)"
$DestinationLibrary = "Claims (Archive)"

# Define the source and destination URLs for the document set
$SourceUrl = "/sites/TransportClaimsDatabase/$SourceLibrary/$DocumentSetName"
$TargetUrl = "/sites/TransportClaimsDatabase/$DestinationLibrary/$DocumentSetName"

Write-Host "Connecting to $siteUrl" -ForegroundColor Yellow 
# Connect-PnPOnline -Url $siteUrl -UseWebLogin
Connect-PnPOnline -Url $siteURL -ClientId $clientId -ClientSecret $clientSecret

#####################################################################################################
$DocumentSetIDToCopy = "16" 

$DocumentSet = Get-PnPListItem -List $SourceLibrary -Id $DocumentSetIDToCopy

    $DocumentSetName = $DocumentSet["FileLeafRef"]
    $SourceUrl = "/sites/TransportClaimsDatabase/$SourceLibrary/$DocumentSetName"
    $TargetUrl = "/sites/TransportClaimsDatabase/$DestinationLibrary/$DocumentSetName"

    Write-Output "`nProcessing Document Set: $DocumentSetName with ID: $DocumentSetIDToCopy"
    Write-Output "SourceUrl: $SourceUrl"
    Write-Output "TargetUrl: $TargetUrl"

    try {
        Copy-PnPFile -SourceUrl $SourceUrl -TargetUrl $TargetUrl -OverwriteIfAlreadyExists -Force
        Write-Output "Successfully copied $DocumentSetName to $DestinationLibrary."
        
    }
    catch {
        Write-Output "Failed to copy $DocumentSetName. Error details:"
        $_ | Format-List -Force
    }
#####################################################################################################

# Disconnect
Disconnect-PnPOnline
Write-Output "Document Sets have been successfully moved from $SourceLibrary to $DestinationLibrary."

This document library containing documents sets, but in code I tried copy .docx file, but I can`t, what strange in the same site collection I have created two test document library, and this script work fine (I change list path and element id). When I created credentials I have used this instruction. And this creds have FullControl permissions, also I have tried do it using SharePoint account, which have have FullControl permissions in all site collections.

I will be glad for a any answer and help

Hello everyone I have tried copy documents in document library, but have mistake: "Access denied". In code bellow I tried copy one element by id:

$clientId = "**************************"
$clientSecret = "**************************"

$siteUrl = "https://***********.sharepoint/sites/TransportClaimsDatabase/"
$SourceLibrary = "Claims (test)"
$DestinationLibrary = "Claims (Archive)"

# Define the source and destination URLs for the document set
$SourceUrl = "/sites/TransportClaimsDatabase/$SourceLibrary/$DocumentSetName"
$TargetUrl = "/sites/TransportClaimsDatabase/$DestinationLibrary/$DocumentSetName"

Write-Host "Connecting to $siteUrl" -ForegroundColor Yellow 
# Connect-PnPOnline -Url $siteUrl -UseWebLogin
Connect-PnPOnline -Url $siteURL -ClientId $clientId -ClientSecret $clientSecret

#####################################################################################################
$DocumentSetIDToCopy = "16" 

$DocumentSet = Get-PnPListItem -List $SourceLibrary -Id $DocumentSetIDToCopy

    $DocumentSetName = $DocumentSet["FileLeafRef"]
    $SourceUrl = "/sites/TransportClaimsDatabase/$SourceLibrary/$DocumentSetName"
    $TargetUrl = "/sites/TransportClaimsDatabase/$DestinationLibrary/$DocumentSetName"

    Write-Output "`nProcessing Document Set: $DocumentSetName with ID: $DocumentSetIDToCopy"
    Write-Output "SourceUrl: $SourceUrl"
    Write-Output "TargetUrl: $TargetUrl"

    try {
        Copy-PnPFile -SourceUrl $SourceUrl -TargetUrl $TargetUrl -OverwriteIfAlreadyExists -Force
        Write-Output "Successfully copied $DocumentSetName to $DestinationLibrary."
        
    }
    catch {
        Write-Output "Failed to copy $DocumentSetName. Error details:"
        $_ | Format-List -Force
    }
#####################################################################################################

# Disconnect
Disconnect-PnPOnline
Write-Output "Document Sets have been successfully moved from $SourceLibrary to $DestinationLibrary."

This document library containing documents sets, but in code I tried copy .docx file, but I can`t, what strange in the same site collection I have created two test document library, and this script work fine (I change list path and element id). When I created credentials I have used this instruction. And this creds have FullControl permissions, also I have tried do it using SharePoint account, which have have FullControl permissions in all site collections.

I will be glad for a any answer and help

Share Improve this question edited Nov 16, 2024 at 16:14 Theo 61.5k8 gold badges27 silver badges46 bronze badges asked Nov 15, 2024 at 19:18 Denys KravchenkoDenys Kravchenko 535 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The problem was that although the document libraries were in the same site, even visually placed next to each other, one list had a path similar to this: https://blabla.sharepoint/sites/SiteName/Lists/ListName/Forms/AllItems.aspx and the other had a path: https://blabla.sharepoint/sites/SiteName/ListName/Forms/AllItems.aspx And because I was using the same path to access both libraries, this error occurred

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far