Feb 24 2023 10:06 AM
Problem is I'm stuck at checking if an item already exists in the list. At the moment it recreates all the items even if they already exist. It doesn't check if the items exists to only create the new ones :(
So if someone could help me a little bit ...that would be great !
Here is my script :
#Parameters $SiteUrl = <my site> $ListName = "Apps" $CSVPath = "MyFile.csv" #Get content $CSVData = Import-CsV -Path $CSVPath -Delimiter ";" #Connection to site Connect-PnPOnline $SiteUrl -UseWebLogin # Iteration through CSV lines ForEach ($Row in $CSVData) { $Item = $ListName.Items | where {$_['Title'] -eq $Row.Title} if($Item -eq $Null){ Write-Host "Adding Application $($Row.Title)" #Adds items Add-PnPListItem -List $ListName -Values @{"Title" = $($Row.Title); "Department"=$($Row.Department); "Project" = $($Row.Project); }; } }
Thanks in advance for your time.
Mar 01 2023 01:47 PM
SolutionHello @Kaddrik,
One of the possible options would be something like this:
#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.
Mar 15 2023 04:24 AM
Mar 01 2023 01:47 PM
SolutionHello @Kaddrik,
One of the possible options would be something like this:
#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.