Monday, 4 July 2016

Insert item to the List from CSV file

<#

Purpose of this script:
-----------------------
To insert as an item to the List from CSV file . Insert process done for both On-Premises and also for Office365 environment.

Run Process
-----------
Open this InsertMasterData_List.ps1 file --> Hit f5 button --> Ask On-Premises (or) Office365 --> Answer the following questions

--> AtLast it shows "Data Added Successfully" else if no data in CSV file it shows "No Data Found" in the output screen.

Important Notes
---------------
Sharepoint List Column names and CSV file Header names should be same.


#>





######################### Add SharePoint PowerShell Snapin ###############################################
 
 if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
 {
 
 Add-PSSnapin Microsoft.SharePoint.Powershell
 
 }
 
########################### End of Add SharePoint PowerShell Snapin ##################################

$question = Read-Host "Are you proceed with On-Premises or Office365? If On-Premises Type ON Else Type OFF for Office365"


 If (($question -eq "ON") -or ($question -eq "on") -or ($question -eq "On") -or ($question -eq "oN"))
{



$SiteURL=Read-Host -Prompt "Please enter the Site URL"

$ListNM=Read-Host -Prompt "Please enter the List Name"

$CSVFileName=Read-Host -Prompt "Please enter Your CSV file Path (forEx:  C:\Users\spadmin\Desktop\CreateItemsToList\MaterData.csv)"

Write-Host "Connected to SharePoint On-Premises site: '$SiteUrl'" -ForegroundColor Green

try
{

$web = Get-SPWeb $SiteURL

 $Lists = $web.Lists[$ListNM]


#Get the CSV file and connect to the SharePoint list

$csvData = import-csv $CSVFileName 



foreach($items in $csvData)
{
$properties = $items | Get-Member -MemberType Properties

     $listContext = $Lists.AddItem()

    for($i=0; $i -lt $properties.Count;$i++)
    {
        $column = $properties[$i]
       
        $colName=$column.Name
              
        #Write-Host $colName
     
        $listContext[$colName] = $items.$colName
    }
     $listContext.Update()
   } 


if($csvData -ne $null)
{
Write-Host "Data Added Successfully !!!"
}
else
{
Write-Host "No Data Found in CSV !!!" -BackgroundColor White -ForegroundColor Red
}
}
catch
{}

}
Elseif (($question -eq "OFF") -or ($question -eq "off") -or ($question -eq "Off") -or ($question -eq "oFF"))
{


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

$SiteURL=Read-Host -Prompt "Please enter the Site URL"

$User = Read-Host -Prompt "Please enter your Login Name"

$Password = Read-Host -Prompt "Please enter your password" -AsSecureString

$ListName=Read-Host -Prompt "Please enter the List Name"

$CSVFileName=Read-Host -Prompt "Please enter Your CSV file Path (forEx:  C:\Users\spadmin\Desktop\CreateItemsToList\MaterData.csv)"

try
{

$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)


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

if (!$Context.ServerObjectIsNull.Value)
{
    Write-Host "Connected to SharePoint Online site: '$SiteUrl'" -ForegroundColor Green
   
    $web = $Context.Web  
    $Context.Load($web)
    $Context.ExecuteQuery()  
    $Lists = $Context.Web.Lists.GetByTitle($ListName)
    $Context.Load($Lists)

    $Context.ExecuteQuery()
 
}

#Get the CSV file and connect to the SharePoint list

$csvData = import-csv $CSVFileName



foreach($items in $csvData)
{
$properties = $items | Get-Member -MemberType Properties

    $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
   
    $listContext = $Lists.AddItem($ListItemInfo)


    # $ni = $Lists.AddItem()

    for($i=0; $i -lt $properties.Count;$i++)
    {
        $column = $properties[$i]
       
        $colName=$column.Name
              
        #Write-Host $colName
     
        $listContext[$colName] = $items.$colName
    }
     $listContext.Update()
     $Context.ExecuteQuery()

}
if($csvData -ne $null)
{
Write-Host "Data Added Successfully !!!"
}
else
{
Write-Host "No Data Found in CSV !!!" -BackgroundColor White -ForegroundColor Red
}
}
catch
{}

}
Else
{
    Write-Host -BackgroundColor White -ForegroundColor Red "INVALID ENTRY! Please try again."
    Break
    #Exit
}

Sample CSV file:













No comments:

Post a Comment