Help With Importing CSV into SharePoint Online - Ignore Blank Values

Lawton Jon 21 Reputation points
2021-11-04T13:25:28.673+00:00

Hi

I have a simple script that imports a CSV and then uses Add-PNPListItem to insert the rows into a SharePoint Online list. All works ok except for when it comes across a blank date/time field.

Having a blank date/time is acceptable in terms of the data structure and is valid but Powershell errors saying it is expecting a value. How can I wrap around an IF statement to ignore writing to that column if there is no value in the CSV?

My script is this:

$credentials = Get-Credential -Message "Please Enter SharePoint Online credentials"
$Site="https://my site collection"
Connect-PnPOnline -Url $Site -Credentials $credentials -UseWebLogin
$CustomerData = Import-CSV "C:\Users\my locaation\thefiletobeimported.csv" -Delimiter '|'
foreach ($Record in $CustomerData){
Add-PnPListItem -List "EDBreaches" -Values @{
"Title"= $Record.'Patient';
"Attend_UID"=$Record.'Attend_UID';
"LeftDepartment"= $Record.'LeftDepartment';
"Arrival"= $Record.'Arrival';
"Triage"= $Record. 'Triage';
"Seen"= $Record.'Seen';
"Departure"= $Record.'Departure';
"ModeOfArrival"= $Record.'ModeOfArrival';
"AllocatedStream"= $Record.'AllocatedStream';
"SourceOfAdmission"=$Record.'SourceOfAdmission';
"InitialComplaint"=$Record.'InitialComplaint';
"WaitMins"=$Record.'WaitMins';
"BreachStatus"=$Record.'BreachStatus';
"PresentingComplaint"=$Record.'PresentingComplaint';
"BreachNumber"=$Record.'BreachNumber';
"BreachReason"=$Record.'BreachReason';
"BedRequired"=$Record.'BedRequired';
"NHSNumber"=$Record.'NHSNumber';
"Disposal"=$Record.'AttendanceDisposalDescription';
"Validation"=$Record.'Validation';
"SecondaryReason"=$Record.'Secondary Reason';
"Comments"=$Record.'Comments';
"ValidatedBy"=$Record.'Validated By'

}
}

Left , Arrival, Triage, Seen and Departure are all date fields that may or may not have a date time. For those that do not have a date/time,. how do I ignore that column. I do not want to pass a default date into the column.

Can anyone help?

Thanks - Jon

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,621 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,363 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 44,776 Reputation points
    2021-11-04T14:30:16.99+00:00

    Try something like this:

    $IgnoreEmpty = "LeftDepartment,Arrival,Triage,Seen,Departure" -split ","
    
    $credentials = Get-Credential -Message "Please Enter SharePoint Online credentials"
    $Site = "https://my site collection"
    Connect-PnPOnline -Url $Site -Credentials $credentials -UseWebLogin
    $CustomerData = Import-Csv "C:\Users\my locaation\thefiletobeimported.csv" -Delimiter '|'
    foreach ($Record in $CustomerData) {
        # always populate these keys with whatever value is in the CSV
        $val = @{
            "Title"               = $Record.'Patient';
            "Attend_UID"          = $Record.'Attend_UID';
            "ModeOfArrival"       = $Record.'ModeOfArrival';
            "AllocatedStream"     = $Record.'AllocatedStream';
            "SourceOfAdmission"   = $Record.'SourceOfAdmission';
            "InitialComplaint"    = $Record.'InitialComplaint';
            "WaitMins"            = $Record.'WaitMins';
            "BreachStatus"        = $Record.'BreachStatus';
            "PresentingComplaint" = $Record.'PresentingComplaint';
            "BreachNumber"        = $Record.'BreachNumber';
            "BreachReason"        = $Record.'BreachReason';
            "BedRequired"         = $Record.'BedRequired';
            "NHSNumber"           = $Record.'NHSNumber';
            "Disposal"            = $Record.'AttendanceDisposalDescription';
            "Validation"          = $Record.'Validation';
            "SecondaryReason"     = $Record.'Secondary Reason';
            "Comments"            = $Record.'Comments';
            "ValidatedBy"         = $Record.'Validated By'
        }
        # only populate these keys if the CSV contains a value
        $IgnoreEmpty |
            ForEach-Object{
                if ($record.$_.Length -gt 0){
                    $val[$_] = $record.$_
                }
            }
       Add-PnPListItem -List "EDBreaches" -Values $val
    }
    

0 additional answers

Sort by: Most helpful