1

I need to copy content of library to another library preserving metadata and few custom columns.
SPFolder.MoveTo works well but it does not preserve the metadata and the additional columns.

How to achieve this?
Is there a better way of doing this than writing a function that traverses the structure recursively? (SOM).

I found some snippets describing how to achieve this for a single file - here my question is if there is something better (build in) because if I will need to copy the content of folder recursively the code will become quite complex and I will have to use recursion.

2 Answers 2

0

As I noticed if the destination library has been created using the template of the source library then MoveTo preserves metadata.

0

Using Powershell:

notice the Replace in script on Author and Editor Field... if same UPS, you dont need the .Replace()

notice the get-qaduser... on folder creation.. maybe you dont need this...

$SourceLibrary = "Documents"
$TargetLibrary = "Documents"

#Get Objects
$SourceWeb = get-spweb https://intranet_A
$TargetWeb = get-spweb https://intranet_B
$SourceFolder = $SourceWeb.GetFolder($SourceLibrary)
$TargetFolder = $TargetWeb.GetFolder($TargetLibrary)
Copy-Files $SourceFolder $TargetFolder

Function Copy-Files($SourceFolder, $TargetFolder)
{
    write-host "Copying Files from:$($SourceFolder.URL) to $($TargetFolder.URL)"
    $badFields = @("PreviewOnForm","ThumbnailOnForm","FileType","ImageSize")
    #Get Each File from the Source
    $SourceFilesColl = $SourceFolder.Files

    #Iterate through each item from the source

    Foreach($SourceFile in $SourceFilesColl)
    {
        #Copy File from the Source
        $NewFile = $TargetFolder.Files.Add($SourceFile.Name, $SourceFile.OpenBinary(),$True)

        #Copy Meta-Data from Source
        Foreach($Field in $SourceFile.Item.Fields)
        {
            If(!$Field.ReadOnlyField)
            {
                if($badFields -notcontains $Field.InternalName)
                {
                    try
                    {
                        if($NewFile.Item.Fields.ContainsField($Field.InternalName))
                        {
                            $NewFile.Item[$Field.InternalName] = $SourceFile.Item[$Field.InternalName]
                        }
                    }
                    catch
                    {
                        Write-Host "$($Field.InternalName) konnte nicht beschrieben werden" -ForegroundColor Yellow
                    }
                }
            }
        }
        $NewFile.Item["Created"] = $SourceFile.Item["Created"]
        $NewFile.Item["Modified"] = $SourceFile.Item["Modified"]
        try
        {
            $NewFile.Item["Author"] = $teamWeb.EnsureUser($SourceFile.Author.UserLogin.Replace("i:05.t|adfs|","i:0e.t|adfs2|"))
        }
        catch
        {
            Write-Host "cant get user" -ForegroundColor Yellow
        }
        try
        {
            $NewFile.Item["Editor"] = $teamWeb.EnsureUser($SourceFile.ModifiedBy.UserLogin.Replace("i:05.t|adfs|","i:0e.t|adfs2|"))
        }
        catch
        {
            Write-Host "cant get user" -ForegroundColor Yellow
        }
        $NewFile.Item.Update()
        #Update


        Write-host "Copied File:"$SourceFile.Name
    }

    Foreach($SubFolder in $SourceFolder.SubFolders)
    {
        if($SubFolder.Name -ne "Forms")
        {
            $NewTargetFolder = $TargetFolder.SubFolders.Add($TargetFolder.ServerRelativeUrl + "/"+ $SubFolder.Name)
            $NewTargetFolder.Item["Modified"] = $SubFolder.Item["Modified"]
            $NewTargetFolder.Item["Created"] = $SubFolder.Item["Created"]
            $AuthorDisplayName = $SubFolder.Item["Author"].Split("#")[1]
            $EditorDisplayName = $SubFolder.Item["Editor"].Split("#")[1]
            $AuthorUPN = Get-QADUser -DisplayName $AuthorDisplayName | Select-Object -ExpandProperty userPrincipalName
            $EditorUPN = Get-QADUser -DisplayName $EditorDisplayName | Select-Object -ExpandProperty userPrincipalName
            $NewTargetFolder.Item["Author"] =  $teamWeb.EnsureUser(("i:0e.t|adfs2|{0}" -f $AuthorUPN))
            $NewTargetFolder.Item["Editor"] = $teamWeb.EnsureUser(("i:0e.t|adfs2|{0}" -f $EditorUPN))
            $NewTargetFolder.Item.Update()
            Copy-Files $SubFolder $NewTargetFolder
        }
    }

}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.