Unable to copy metadata

Viewed 478

BACKGROUND:

I have the following script which should copy a single file from one location to another and also copy the Modified date metadata.

It all works except it does not copy the modified date metadata and shows the date/time the file was copied across in the modified date metadata field/column:

When I output the source files modified date to the screen, it shows the correct modified date, but that does not seem to get applied to the file in the new location.


SCRIPT:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables for Processing
$WebURL="https://sharepoint.oshirowanen.com/sites/oshirodev"
$SourceFile="https://sharepoint.oshirowanen.com/sites/oshirodev/folder1/user1/test.doc"
$TargetLibrary="folder2"

#Get Objects
$Web = Get-SPWeb $WebURL
$SourceFile = $Web.GetFile($SourceFile)
$TargetLibrary = $Web.GetFolder($TargetLibrary)

#Copy the file into the Target library
$File = $TargetLibrary.Files.Add($SourceFile.Name, $SourceFile.OpenBinary(), $true)

#Copy Meta-Data
#METADATA UPDATE DOES NOT WORK
$item = $File.Item

#Original attempt - not working
#$item["Modified"] = $SourceFile.TimeLastModified.ToLocalTime()

#Suggested attempt 2 - not working
#$itemModified = ([DateTime]$SourceFile.Item["Modified"]).DateTime 
#$item["Modified"] = $itemModified 

#Suggested attempt 2
$File.item["Modified"] = $SourceFile.item["Modified"]

##Check value of sourcefiles modifieddate - shows correct modified date
write-host $SourceFile.item["Modified"]

#Update
$item.UpdateOverwriteVersion()

OUTPUT:

Shows current date as modified date

However, the write-host shows the correct modified date:

PS C:\Users\Oshiro\Desktop\scripts> .\MetaData.ps1
10 August 2018 10:00:07
PS C:\Users\Oshiro\Desktop\scripts>

QUESTION:

Can anyone see what I have done wrong?


EXPORT-SPWEB ATTEMPT:

Script:

Export-SPWeb -Identity "https://sharepoint.oshirowanen.com/sites/oshirodev/folder1/user1/" -ItemUrl "lists/Customlist" -Path "c:\sharepoint_export\customlist_export.cmp" -IncludeUserSecurity

Error:

Export-SPWeb : Cannot find an SPWeb object with Id or Url : https://sharepoint.oshirowanen.com/sites/oshirodev/folder1/user1/.
At line:1 char:13
+ Export-SPWeb <<<<  -Identity "https://sharepoint.oshirowanen.com/sites/oshirodev/folder1/user1/" -ItemUrl "lists/Customlist" -Path "c:\sharepoint_export\customlist_export.cmp" -IncludeUserSecurity
    + CategoryInfo          : InvalidData: (Microsoft.Share...CmdletExportWeb:SPCmdletExportWeb) [Export-SPWeb], SPCmdletPipeBindException
    + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletExportWeb
1 Answers

Your script appears fine to me, only one line should be changed (these columns are usually considered read-only but they can be modified):

# new_file["Modified"] = original_file["Modified"]
$File.item["Modified"] = $SourceFile.item["Modified"]

You will find some topics concerning this issue. Some posts will tell you that export-spweb and import-spweb do not preserve such items as create/modified date, created by, etc. which is now not true.

Besides fixing the line could you check these:

  • GUI IncludeUserSecurity

Enable IncludeUserSecurity via GUI. You need to: check box on central administration console/Backup Section -> 'Export Full Security'

Export Full Security section

  • If you should use Export/Import-SPWeb, there is an option -IncludeUserSecurity.

You could script it using Export-SPWeb and Import-SPWeb which do contain the parameter [-IncludeUserSecurity]

Edit - due to comments - using Export-SPWeb to export only a list of files

You can't export, as oshirowanen correctly noted, individual files with Export-SPWeb - you can only export: whole site, list or library.

I'm including a way how to export/import a list:

Exporting a list via Export-SPWeb (site collection is not root):

Export-SPWeb -Identity "https://sharepoint.oshirowanen.com/sites/oshirodev/folder1/user1/" -ItemUrl "lists/Customlist" -Path "c:\sharepoint_export\customlist_export.cmp" -IncludeUserSecurity

Note: Don't forget to include the last backslash at the site collection name.

(To create: a list in SharePoint, to edit a list or create a custom list via Sharepoint designer).

To import the list should be straightforward:

Import-SPWeb "https://sharepoint.oshirowanen.com/sites/oshirodev/folder2/" -Path "c:\sharepoint_export\customlist_export.cmp" -IncludeUserSecurity
Related