Denys Kravchenko
Denys Kravchenko

Reputation: 53

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

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.com/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

Upvotes: 0

Views: 49

Answers (1)

Denys Kravchenko
Denys Kravchenko

Reputation: 53

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.com/sites/SiteName/Lists/ListName/Forms/AllItems.aspx and the other had a path: https://blabla.sharepoint.com/sites/SiteName/ListName/Forms/AllItems.aspx And because I was using the same path to access both libraries, this error occurred

Upvotes: 0

Related Questions