I have written a script that imports data into a from a specified CSV file to a list in a specified SharePoint 2013 site (on premise). However, when I come to run the script, I get these two errors:
I understand why I am getting these errors. It is because the column in the CSV file which holds Date fields contains blanks. I would put these blanks to a date however, there is another column in SharePoint where if it's value is "1" that column MUST NOT contain a date.
What can I add to my script to fix this issue?
Thanks in advance, Sam D Harris
@Sam I am sharing the code for adding date data from CSV to sharepoint list using powershell.
In your foreach loop which reads every line of imported CSV.
[DateTime]$loggedOnDate = New-Object System.DateTime;
$newItem = $list.items.add();
if([DateTime]::TryParse($item."Logged On", [ref]$loggedOnDate)){
$newitem["LoggedOn"] = $loggedOnDate;
}
$newitem.update();
Where :- $item."Logged On" - "Logged On" is column name in CSV file. and $newitem["LoggedOn"] is column name in splist.
Now lets start anwering your query with assumption that CSV has two columns
Powershell code to add csv data in sharepoint would be.
$newItem = $list.items.add();
IF(($item.Active -ne 1)
{
[DateTime]$loggedOnDate = New-Object System.DateTime;
if([DateTime]::TryParse($item."Logged On", [ref]$loggedOnDate)){
$newitem["LoggedOn"] = $loggedOnDate;
}
}
$newitem.update();
Make sure default value for Date column in sharepoint list should be blank.