Forum Discussion
Automatic Data import
Hi,
Using csv is simplest method but you need to create list columns with corresponding csv header name.then try below script, here i create simple script with two column and two csv header so you can change column name and header value in the script and this script based on pnp sharepoint online powershell module so you need to install pnp sharepoint online powershell module .
$cred= Get-Credential
$Listname= "Listname"
Connect-PnPOnline -Url https://TenantName.sharepoint.com/sites/contosobeta -Credentials $cred
$csv = Import-Csv "C:\Users\csv\users2.csv"
$ctx=Get-PnPContext
$list= Get-PnPList -Identity $Listname
foreach ($line in $csv)
{
$listItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$listItem = $list.AddItem($listItemInfo)
$listItem["Listcolumn1"] = $line.Csvheadername1
$listItem["Listcolumn2"] = $line.Csvheadername2
$listItem.Update()
$ctx.load($list)
$ctx.executeQuery()
}Note:
Before run the script youneed modify following value in the script
$Listname= "Listname"
$listItem["Listcolumn1"] = $line.Csvheadername1 $listItem["Listcolumn2"] = $line.Csvheadername2
- Apr 06, 2017
Your script will work for simple text fields but what if the SharePoint fields are of a diffferent type.
Also using Add-PnPListItem and Set-PnPListItem and Get-PnPListItem will be better.
Also you would need to handle things like "the item already exists"....
- Manidurai MohanamariappanApr 06, 2017Iron Contributor
Hi pieter,
thanks for info, i already tried with Add-PnPListItem but it's make trobule so that i used csom within pnp script .
- Apr 06, 2017
Use Add-PnPListItem without the Value parameter. Then use Set-PnPListItem to set the fields. This is a known issue.
https://github.com/SharePoint/PnP-PowerShell/issues/778