Skip to content
    Categories
    Select Category

    Welcome to Teams Tuesday

    A podcast about mastering the best of what other people have already figured out with M365.

    Teams Tuesday focuses on insights and lessons that never expire. You’ll walk away from every episode with actionable insights that help you get better results and be more productive.

    Copying files to another library in another site or site collection

    1 min read

    One limitation of SharePoint designer, is that it can only copy files from a library to another library in the same site. This does have a purpose, but this is isn’t really an archiving solution.

    The following code copying files (and metadata) from view in a library to another library in another site or site collection.

    $web = Get-SPWeb “http://sp10/

    $listnamefrom=”Testfrom”

    $viewName=”View1″

    $listnameTo=”TestTo”

     

    $ver = $host | select version

    if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = “ReuseThread”}

     

    if ((Get-PSSnapin “Microsoft.SharePoint.PowerShell” -ErrorAction SilentlyContinue) -eq $null)

    {

    Add-PSSnapin “Microsoft.SharePoint.PowerShell”

    }

     

    $web = Get-SPWeb “http://sp10/”

    $listnamefrom=”Testfrom”

    $viewName=”View1″

    $listnameTo=”TestTo”

    $list = $web.Lists[$listnamefrom]

    $view = $list.Views[$viewName]

    $columns=New-Object System.Collections.ArrayList

    $dlist = $web.Lists[$listnameTo]

    foreach ($col in $view.ViewFields)

    {

    if($col -ne “DocIcon” -and $col -ne “LinkFilename” -and $col -ne “Modified” -and

    $col -ne “Editor”)

    {

    $columns.add($col)

    }

    }

     

    $items = $list.GetItems($view)

    #$AllFolders = $sList.Folders

    $RootFolder = $list.RootFolder

    $RootItems = $RootFolder.files

    foreach($RootItem in $RootItems)

    {

    write-host $RootItem.Name

    $sBytes = $RootItem.OpenBinary()

    $dFile = $dList.RootFolder.Files.Add($RootItem.Name, $sBytes, $true)

    #$AllFields = $RootItem.Item.Fields | ? {!($_.sealed)}

     

    foreach($Field in $columns)

    {

    $dFile.AddProperty($Field, $RootItem.Properties[$Field])

    }

    $dFile.Update()

    write-host “Updated”

    }

    $web.Dispose()

    write-host “Done”

     

    #$items


     

    Comments are closed.