I have excel file and i want to import excel sheet data into SharePoint list if not exist. Using PowerShell how can i achieve this?
1 Answer
You can use PowerShell to first create a SharePoint list and import csv file to that list
#Specify tenant admin and site URL
$User =
$SiteURL =
$ListTitle =
$ImportFile=
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds
#Retrieve lists
$Lists = $Context.Web.Lists
$Context.Load($Lists)
$Context.ExecuteQuery()
$list = $Lists | where{$_.Title -eq $ListTitle}
if ($list -eq $Null ) { write-host "List Does not Exist"
#Create list with "custom" list template
$ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$ListInfo.Title = $ListTitle
$ListInfo.TemplateType = "100"
$List = $Context.Web.Lists.Add($ListInfo)
$List.Description = $ListTitle
$List.Update()
$Context.ExecuteQuery()
#Add fields to the list
$List.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='Event Title'/>",$true,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
$List.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='Event Description'/>",$true,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
$List.Update()
$Context.ExecuteQuery()
}
This script create new list if there is no existing list have the name your referred. Remember to add needed information and change the filed name and type
For import csv file to SharePoint online list, you can check this blog: http://www.sharepointdiary.com/2015/09/import-csv-file-to-sharepoint-list-using-powershell.html