Load CSV File Value Into SharePoint List Using Powershell Script

Introduction

In this blog, you will learn how to load CSV file values into SharePoint list, using PowerShell Script.

Prerequisite

Create a SharePoint custom list with the following site columns:

  • SalesMaster List with Title Column
  • ServiceMaster List with Title Column
  • RegionMaster List with Title Column
  • TestMaster List

    Title - Single line of text

    Subject - Single line of text

    Sales - Lookup value to SalesMaster List

    Service - Lookup value to ServiceMaster List

    Region - Lookup value to RegionMaster List

    table

Steps Involved

There are several different ways to import the data from a Spreadsheet to a SharePoint list. Depending on your requirements, you can copy the data into SharePoint lists. Add the list items involved in manual work in SharePoint. For huge number of items, it's very complex work. We can put the same content CSV file and write the code to add to SharePoint lists programmatically. The following code snippet allows you to add CSV files to a SharePoint list.

  1. Add the reference to Microsoft.SharePoint.PowerShell, if it is not not already added.
    1. #Setup the correct modules  
    2. for SharePoint Manipulation  
    3. if ((Get - PSSnapin - Name Microsoft.SharePoint.PowerShell - ErrorAction SilentlyContinue) - eq $null) {  
    4.     Add - PsSnapin Microsoft.SharePoint.PowerShell  
    5. }  
    6. $host.Runspace.ThreadOptions = "ReuseThread"  
  2. Open the Web, using Get-SPWeb object.
    1. #Open SharePoint List   
    2. $SPServer="http://siteURL.com/"   
    3. $spWeb = Get-SPWeb $SPServer   
  3. Get the list, using GetList method.
    1. $SPAppList="/Lists/TestMaster/"   
    2. $spData = $spWeb.GetList($SPAppList)   
  4. Give the local location of CSV file.

    $InvFile="D:\Users\ImportCSVDataToLists\TestMaster.csv"

  5. Test the path of CSV file, using Test-Path. If it exists, load the file, using Import-CSV otherwise exit the PowerShell script.
    1. #Get Data from Inventory CSV File  
    2. $FileExists = (Test - Path $InvFile - PathType Leaf)  
    3. if ($FileExists) {  
    4.     "Loading $InvFile for processing..."  
    5.     $tblData = Import - CSV $InvFile  
    6. else {  
    7.     "$InvFile not found - stopping import!"  
    8.     exit  
    9. }  
  6. Loop through all the items in CSV file, using the for each loop.

    Add the item to the list, using AddItem method.

    Add the look up column by getting the look up list item ID and the value. Add all the look up columns in the same way.
    Update the Item to complete the AddItem function.

    # Loop through Applications add each one to SharePoint

    "Uploading the data to SharePoint...."
    1. foreach($row in $tblData)   
    2. {  
    3.     "Adding entry for " + $row.  
    4.     "Title".ToString()  
    5.     $spItem = $spData.AddItem()  
    6.     $spItem["Title"] = $row.  
    7.     "Title".ToString()  
    8.     $spItem["Subject"] = $row.  
    9.     "Claim".ToString()  
    10.     $looValue1 = $row.  
    11.     "Sales".ToString()  
    12.     $LookupList1 = $spWeb.Lists["SalesMaster"];  
    13.     $LookupItem1 = $LookupList1.Items | Where - Object {  
    14.         $_.Title - eq $looValue1  
    15.     }  
    16.     $Lookup1 = ($LookupItem1.ID).ToString() + ";#" + ($LookupItem1.Title).ToString()  
    17.     $spItem["Sales"] = $Lookup1  
    18.     $looValue2 = $row.  
    19.     "Service".ToString()  
    20.     $LookupList2 = $spWeb.Lists["ServiceMaster"];  
    21.     $LookupItem2 = $LookupList2.Items | Where - Object {  
    22.         $_.Title - eq $looValue2  
    23.     }  
    24.     $Lookup2 = ($LookupItem2.ID).ToString() + ";#" + ($LookupItem2.Title).ToString()  
    25.     $spItem["Service"] = $Lookup2  
    26.     $looValue3 = $row.  
    27.     "Region".ToString()  
    28.     $LookupList3 = $spWeb.Lists["RegionMaster"];  
    29.     $LookupItem3 = $LookupList3.Items | Where - Object {  
    30.         $_.Title - eq $looValue3  
    31.     }  
    32.     $Lookup3 = ($LookupItem3.ID).ToString() + ";#" + ($LookupItem3.Title).ToString()  
    33.     $spItem["Region"] = $Lookup3  
    34.     $spItem.Update()  
    35. }  
    36. "---------------"  
    37. "Upload Complete"  
  7. Finally, dispose spWeb object.

    $spWeb.Dispose()

Summary

Thus, you have learned, how to load CSV file value into SharePoint list, using PowerShell Script.