Forum Discussion
Kaddrik
Feb 24, 2023Copper Contributor
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...
- Mar 01, 2023
Hello Kaddrik,
One of the possible options would be something like this:
- Get SharePoint List Items
- Get CSV file
- Compare two collections and identify the diff, items that are present in CSV but are not present in SharePoint list
- 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.
AndySvints
Mar 01, 2023Iron Contributor
Hello Kaddrik,
One of the possible options would be something like this:
- Get SharePoint List Items
- Get CSV file
- Compare two collections and identify the diff, items that are present in CSV but are not present in SharePoint list
- 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.
- KaddrikMar 15, 2023Copper ContributorHi AndySvints,
Thanks a lot for your reply. I was like desperate for someone to be able to help me on this! 😄
I just did a quick test on a test list, cause in the meantime i went on with my project so i'll come back to this later, but it seems to be working !!! 🙂
So thanks a lot for your time and ...i hope i can come back to you if i see there is something wrong when i 'll do it on my original list 😛
Thanks a lot !!!