0

I'm using Export-SPWeb to export list from one Sharepoint site collection and Import-SPWeb to import this list to another site collection.

I need to do it regularly and that the list and all data in it would be overwritten every time.

Now the list is always updated - items are appended to the list so the same data repeats every time on new import.
Both sites are project sites, both list has versioning enabled. Where else could be the problem???

I'm using these PowerShell commands:

Export-SPWeb -Identity <Site URL> -Path <string> -ItemUrl "<list path>" -Force

Import-SPWeb -Identity <Another Site URL> -Path "file.cmp" -Force -UpdateVersions Overwrite

EDIT:

So I ended up using this PowerShell script:

try {
    # Export the list
    Export-SPWeb -Identity <Site URL> -Path <string> -ItemUrl "<list path>" -Force

    # Get the destination web and delete the list
    $web = Get-SPWeb "<Site URL>"
    $listToDelete = $web.Lists["List name"]
    
    if ($listToDelete -ne $null) {
        $web.Lists.Delete($listToDelete.ID)
        Write-Host "List deleted successfully."
    } else {
        Write-Host "List not found on the destination web."
    }

    # Import the list
    Import-SPWeb -Identity <Site URL> -Path "file.cmp" -Force -UpdateVersions Overwrite

    Write-Host "List imported successfully."
}
catch {
    Write-Host "An error occurred: $_.Exception.Message"
}
finally {
    # Dispose of the SPWeb object
    if ($web -ne $null) {
        $web.Dispose()
    }
}
3
  • Try deleting the list on the destination site collection before running Import-SPWeb You can use Remove-PnPList -Identity "<list name>" -Recycle -Force Commented Feb 1, 2024 at 21:32
  • I don't use PnP also Remove-SPList gives error that is not recognized as the name of a cmdlet. Commented Feb 2, 2024 at 8:51
  • Well you got that error because PnP PowerShell isnt installed, install it here: pnp.github.io/powershell Commented Feb 2, 2024 at 16:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.