Forum Discussion

Kaddrik's avatar
Kaddrik
Copper Contributor
Feb 24, 2023
Solved

Import non existing CSV rows to SharePoint online list

Hello, Hope you're doing good.   I'm very new to Powershell and try to import CSV rows into a SharePoint online list. I found several things on the net and combined them all together so what i'm...
  • AndySvints's avatar
    Mar 01, 2023

    Hello Kaddrik,

    One of the possible options would be something like this:

    1. Get SharePoint List Items
    2. Get CSV file
    3. Compare two collections and identify the diff, items that are present in CSV but are not present in SharePoint list
    4. Foreach diff item add it to the SharePoint

     

    #Parameters
    $SiteUrl = "https://m365x45097644.sharepoint.com/sites/Contoso"
    $ListName = "ProjectList"
    $CSVPath = "MyFile.csv"
    
    #Get content
    $CSVData = Import-CsV -Path $CSVPath #-Delimiter ";"
    
    #Connection to site
    Connect-PnPOnline $SiteUrl -UseWebLogin
    
    $ListItems=(Get-PnPListItem -List $ListName ).FieldValues | Select-Object @{l="Title";e={$_."Title"}} , @{l="Department";e={$_."Department"}}, @{l="Project";e={$_."Project"}}
    
    $Diff=Compare-Object -ReferenceObject $ListItems -DifferenceObject $CSVData | Where-Object {$_.SideIndicator -eq "=>"} | Select-Object -ExpandProperty InputObject
    
    ForEach ($Row in $Diff)
    {
        Write-Host "Adding Application $($Row.Title)"
        #Adds items
        Add-PnPListItem -List $ListName -Values @{"Title" = $($Row.Title);
                                                  "Department"=$($Row.Department);
                                                  "Project" = $($Row.Project);
       }
    } 

    Hope that helps.

Resources