question

AlexHannah-6224 avatar image
0 Votes"
AlexHannah-6224 asked EchoDu-MSFT commented

PnP PowerShell - Upload CSV to SharePoint List

Hi all,

I'm looking for some help with PnP PowerShell. I have a script which takes a CSV file and uploads each row as an item to SharePoint Online (see below). My question is, how do I get the script to skip items that already exist in the SharePoint Online list? Currently the script just uploads all rows within the CSV and creates some duplicates.


Parameters


$SiteUrl = "{site}"

$ListName = "iPhone"

$CSVPath = "C:\Users{user}\Documents\PowerShell\Upload SharePoint items from CSV\iphoneexport.csv"


Get the CSV file contents


$CSVData = Import-CsV -Path $CSVPath


Connect to site


Connect-PnPOnline $SiteUrl -Interactive


Iterate through each Row in the CSV and import data to SharePoint Online List


ForEach ($Row in $CSVData)

{

 Write-Host "Adding Item $($Row.'Asset Number')"

     

 #Add List Items - Map with Internal Names of the Fields!

 Add-PnPListItem -List $ListName -Values @{"Title" = $($Row.'Asset Number');

                         "User" = $($Row.User);

 };

}



I'd be grateful for any advice anyone can give.

Many thanks,

Alex.

office-sharepoint-onlinewindows-server-powershellsharepoint-dev
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@AlexHannah-6224,

I am currently looking into this issue and will give you an update as soon as possible.

Thank you for your understanding and support.

0 Votes 0 ·

1 Answer

JoyZ avatar image
0 Votes"
JoyZ answered EchoDu-MSFT commented

@AlexHannah-6224,

We can check the item by CAML+ PNP PowerShell, if the Title in list already exists, it will be ignored, check as following:

 $Username='julie@tenant.onmicrosoft.com'
 $Password = 'Doh76594'
    
 #region Credentials
 [SecureString]$SecurePass = ConvertTo-SecureString $Password -AsPlainText -Force 
 [System.Management.Automation.PSCredential]$PSCredentials = New-Object System.Management.Automation.PSCredential($Username, $SecurePass) 
 #endregion Credentials
    
 #connect to site
 $consite=Connect-PnPOnline -Url 'https://tenant.sharepoint.com/sites/Team1' -Credentials $PSCredentials
    
 $listName = "list1022" 
 $FilePath= "C:\Temp\Test1028.csv"
 $CSVData = Import-CsV -Path $FilePath
    
    
 Import-Csv -Path $FilePath|%{
    
     $checkitem = $null
     $tarTitle= $_."Asset Number"
     $tarUser = $_.User
        
 $caml=@"
     <View>  
         <Query> 
             <Where><Eq><FieldRef Name='Title' /><Value Type='Text'>$tarTitle</Value></Eq></Where> 
         </Query> 
     </View>
 "@
    
    $checkitem= Get-PnPListItem -List $listName -Query $caml
        
    if($checkitem){
         Write-Host "this item exists:" $($tarTitle)
    }else{
         Write-Host "this item does not exist:" $($tarTitle)
         Add-PnPListItem -List $ListName -Values @{"Title" = $($tarTitle);
                          "Active" = $($tarUser );}
    }
 }

Before:
144353-image.png

After:

144413-image.png
Simialr issue for your reference:

https://social.msdn.microsoft.com/Forums/en-US/e0dac7d9-36fe-4c6d-bccc-13aa4474bc37/check-if-listitem-exist-using-pnp-powershell?forum=sharepointdevelopment


If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.





image.png (30.2 KiB)
image.png (37.0 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@AlexHannah-6224,

Is there any progress on this issue?

Please feel free to reply.

0 Votes 0 ·

@AlexHannah-6224,

Do you have any progress on this issue?

Please remember to update this thread if you need further assistance.

0 Votes 0 ·

Hello @AlexHannah-6224,

If Julie's reply helps you, please remember to accept her reply as answer via the "Accept Answer" button, it will be beneficial to others in this forum who meet the same issue in the future. Thanks for your understanding and cooperation.

Thanks,
Echo Du

0 Votes 0 ·