0

I have the following powershell script to create a copy of a item:-

$web = get-spweb "http:*********"
$list = $web.lists["Action"]
$list2 = $web.lists["Risk Issue"]
foreach ($i in $list.items)
{
$a = $i["ID"];
$sourceItem = $list.items.GetItemById($a)

$versions = $sourceItem.versions;

$newItem = $list2.items.Add();

$versionCount = $versions.Count;

##We need a for loop here as we must work backwards through the version collection
for ($i = $versionCount;$i -gt 0;$i--)
{
    $version = $versions[$i-1];


    $newItem["Modified"] = $version["Modified"];

    $newItem["Created"] = $version["Created"];


    $newItem["Date_x0020_Reported"] = $version["Date_x0020_Reported"];



    $newItem.Update();

}
}

now all the values inside the $newItem were copied successfully , except for a site column named "Date Reported" as shown inthe above script , which will have a value equal to the $version["Date_x0020_Reported"] minus 1 day. so if $version["Date_x0020_Reported"] is 18/11/1990 the value inside the $newItem["Date_x0020_Reported"] will be 17/11/1990 not sure why, although the other date-related fields such as modified & created were copied successfully . here is my site column definition:-

enter image description here

  • 1
    My gut reaction to this is that it has something to do with time zones / daylight savings time / etc. Something where there is a time offset that was calculated differently for your old items than it is for your new items. Try looking into the raw values of the old date fields and see if you can discover anything. Is the old Date Reported field date only, like your new one, or does it include time as well? – Dylan Cristy Nov 30 '15 at 17:33
  • @DylanCristy both are of type "Date Only".. – john Gu Nov 30 '15 at 17:59
  • 1
    It's not that it's necessarily DST, it's just that DST is yet another thing that can be a factor in calculating the time offset from UTC. As far as the time settings, I would check the system time settings (time zone, etc.) for the live server, and there are also time zone settings for the web app in SharePoint (you can check through Central Admin). Also, if you are running from a restore...is the backup from a day ago? Is it possible that the backup being "frozen" for a day before being restored introduced the offset? – Dylan Cristy Nov 30 '15 at 19:11
  • 1
    (I am not asking because I think I know a solution, these are just the kinds of questions I would be looking into if I were having this problem.) – Dylan Cristy Nov 30 '15 at 19:12
  • 1
    I've already had a similar issue and it was due to a transformed date in UTC so 2 hours were substract and 2015-12-01 00:00 become 2015-11-30 22:00. Maybe you have the same issue – user1898765 Dec 1 '15 at 13:47
-1

Hope I understood you, you want to subtract 1 day from original list item Date and then save and you date is not getting copied as of now

Please try this

$newItem["Date_x0020_Reported"] = $version["Date_x0020_Reported"].AddDays(-1);

this will work, I have tried and it updates value to datetime column perfectly

  • no i do not want to substract 1 day,, as i am getting this faulty behavioure without any effort and i am asking why !! – john Gu Dec 1 '15 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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