0

I have following powershell to create new list item from a CSV file and it is working perfectly fine in SP Online environment.

we are making some changes in this list to have the Title field hold unique value only...

Now I want to create the log of all successful and failed upload via this powershell script.

I have no clue to this, any help will be appreciated.

    Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\BIN\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\BIN\Microsoft.SharePoint.Client.Runtime.dll"

##Variables for Processing
$SiteUrl = "https://COMPANY NAME.sharepoint.com/sites/SITENAME"
$ListName = "SAMPLE LIST NAME"
$ImportFile = "D:\CSVFILE.csv"
$User = "USER EMAIL ID"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString

#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds

#Get the List
$List = $Context.web.Lists.GetByTitle($ListName)

#Get the Data from CSV and Add to SharePoint List
$data = Import-Csv $ImportFile
Foreach ($row in $data) {

    #add item to List
    $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
    $Item = $List.AddItem($ListItemInfo)
    $Item["Title"] = $row.Title
    $Item["Department"] = $row.Department
    $Item["EmpIDBH"] = $row.EmpIDBH
    $Item["Location"] = $row.Location
    $Item["IsActive"] = $row.IsActive

    $Item.Update()
    $Context.ExecuteQuery() 

}
Write-host "CSV data Imported to SharePoint List Successfully!"
0

You could try to add Try Catch blocks in your script.

For example:

Foreach ($row in $data) {

    #add item to List
    $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
    $Item = $List.AddItem($ListItemInfo)
    $Item["Title"] = $row.Title
    $Item["Department"] = $row.Department
    $Item["EmpIDBH"] = $row.EmpIDBH
    $Item["Location"] = $row.Location
    $Item["IsActive"] = $row.IsActive

    $Item.Update()
    try{
      $Context.ExecuteQuery()
     write-host $("successful upload: "+$row)
    }
    catch{
      write-host $("failed upload: "+$row)  
    } 

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.