8

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.

script:

$currentItem.Editing.BeginEdit();
$currentItem["Publish Date"] = $row."publish date"
$currentItem.Editing.EndEdit();

screenshot after running script:

enter image description here

1
  • How do you get $currentItem? Do you edit the item in the correct language? Commented Jan 12, 2019 at 6:35

6 Answers 6

4

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:

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

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.Editing.BeginEdit()
$currentItem["Publish Date"] = $row."publish date"
$currentItem.Editing.EndEdit()

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

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

To read date Field from CSV file. You need to convert it to date format and then update the sitecore date field. You need to update time format(dd-MM-yyyy HH:mm:ss) below same as the format you read from the csv file.

    $csvDate = [DateTime]::ParseExact($row.PublishDate, "dd-MM-yyyy HH:mm:ss", [Globalization.CultureInfo]::GetCultureInfo("en-US"))
    $item["publishdate"] = [Sitecore.DateUtil]::ToIsoDate($csvDate)
1

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)
0

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")
}
0

Update your date script code as below:

 #Add leading zero into the day and month to avoid error in ParseExact
        if($row.CreatedDate.Trim() -ne "")
        {
            $oldDate = $row.CreatedDate.Trim().Replace('/','-')
            $newDate = $oldDate.Split("-")[0].ToString().PadLeft(2, '0')+'-'+$oldDate.Split("-")[1].ToString().PadLeft(2, '0')+'-'+$oldDate.Split("-")[2].ToString().PadLeft(2, '0')
            $csvDate = [DateTime]::ParseExact($newDate,"dd-MM-yyyy",[CultureInfo]::InvariantCulture)

            $item["PublishDate"] = [Sitecore.DateUtil]::ToIsoDate($csvDate)     
        }

In the CSV file if the Date Column field values will be separated with - then if would word fine. If the date column field value will be separated with / then the code will return a null error. So in the above code, I have trimmed the CreatedDate field and replace the '/' with '-' and do remember to add leading zero into the day, month to avoid error in ParseExact.

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.