I would like to know how to update date field from CSV file.Attached the code while debugging I can able to publish date is updated but its not visible in Content tree.


$currentItem["Publish Date"] = $row."publish date"

screenshot after running script:

enter image description here

  • How do you get $currentItem? Do you edit the item in the correct language? – Tamás Tárnok Jan 12 '19 at 6:35

As far as editing an item, your script looks to be doing what it needs to, but I'm guessing the actual date value is not something Sitecore recognized for a Date type field. Depending on the date format your CSV contains, you will likely need to utilize the Sitecore.DateUtil class to parse it into something that field can accept:

$csvDate = [DateTime]::ParseExact($row."publish date","dd/MM/yyyy HH:mm:ss",[Globalization.cultureinfo]::GetCultureInfo("en-US"))
$currentItem["Publish Date"] = [Sitecore.DateUtil]::ToIsoDate($csvDate)
  • got this error message Exception calling "Parse" with "1" argument(s): "String was not recognized as a valid DateTime." – sindu Jan 13 '19 at 12:52
  • @sindu I updated my answer to include the exact parsing format from your answer – vandsh Jan 13 '19 at 13:13

In Sitecore, when you need to edit an item using the Sitecore API, you first need to put the item in Editing mode. To do this, you'll can call .Editing.BeginEdit() before attempting to modify the item. Then end it with .Editing.EndEdit().

$currentItem["Publish Date"] = $row."publish date"

You can also format your call like this (brackets removed) and SPE will take care of all the necessary usages of .Editing.BeginEdit and .Editing.EndEdit:

$currentItem."Publish Date" = $row."publish date"

Additional information on working with items using SPE can be found on the SPE Documentation site

  • I tried with editing only its not working – sindu Jan 11 '19 at 21:44
  • Are you able to edit any other fields? Single-Line text field for example. – Gabe Streza Jan 11 '19 at 22:15

This format is working for me:

$csvDate = [datetime]::ParseExact("23/09/2015 05:41:27","dd/MM/yyyy HH:mm:ss",[Globalization.cultureinfo]::GetCultureInfo("en-US"))        
$currentItem["Publish Date"] = [Sitecore.DateUtil]::ToIsoDate($csvDate)

This format is working for me - I had to add ".Value" to the item reference.

if ( $ValidFrom" -ne "" ) {
    $itemEdit.Fields["__Valid from"].Value = [Sitecore.DateUtil]::ToIsoDate($ValidFrom")

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.