Search This Blog

Monday, December 8, 2014

Import List Items in SharePoint using Powershell

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

$csvVariable= Import-CSV -path "http://sp2010.contoso.com/finance/"

# Destination site collection
$WebURL = "http://sp2010.contoso.com/sites/Utility/" 

# Destination list name
$listName = "Inventory" 

#Get the SPWeb object and save it to a variable
$web = Get-SPWeb -identity $WebURL 

#Get the SPList object to retrieve the list
$list = $web.Lists[$listName] 

#Get all items in this list and save them to a variable 
$items = $list.items 

      #loop through csv file
foreach($row in $csvVariable)
{
    $updated = 0
    #loop through SharePoint list
    foreach($item in $items)
    {
        if($item["ID #"] -eq $row."ID #") 
            { 
                $updated++
            } 
     }
        #add new item if an update wasn't made
        if($updated -eq 0)
            {
$newItem = $list.items.Add()
$newItem["Product"] = $row."Product"s
$newItem["Quantity"] = $row."Quantity" 
$newItem["Vendor"] = $row."Vendor"
$newItem["Department"] = $row."Vendor"
$newItem["ID #"] = $row."ID #"
$newItem["Department"] = $row."Department"      
                $newItem.Update()
            } 
}
$web.Dispose() 

No comments:

Post a Comment