#Get the CSV file and connect to the SharePoint list
$vessellist = import-csv -Path
"C:\Temp\VesselInPortReport.csv"
$itemCount = $vessellist.Count;
$currentItem = 1;
foreach
($item
in
$vessellist)
{
#Update the progress information
Write-Progress -Id 1 -ParentId 0 -Activity
"Listing Data In CSV File"
-PercentComplete (($currentItem/$itemCount)*100) -Status
"Item $currentItem or $itemCount"
;
$currentItem++;
#Write the rows VESSEL_NAME column to the console
Write-Host $item.VESSEL_NAME;
}
#Get the list used to import the data to
$l = (Get-Spweb
"http://corporate"
).GetList(
"http://corporate/Lists/VesselsInPort"
)
#Get the lists EmployeeType field (choice)
$employeeType = $l.Fields[
"EmployeeType"
] -
as
[Microsoft.SharePoint.SPFieldChoice]
#Loop through the items and add them to the list
$r = 1;
"Importing Data From CSV into SharePoint"
"Adding item $currentItem or $itemCount"
$ni = $l.items.Add();
#Add the Title, using the rows VESSEL_NAME column
$ni[
"Title"
] = $item.VESSEL_NAME;
#Add the "Date Recorded" field, using the csv rows "RPT_DATE" column
[DateTime]$rd = New-Object System.DateTime;
if
([DateTime]::TryParse($item.RPT_DATE, [
ref
]$rd)){
"Date Recorded"
] = $rd;
#Add the csv rows "TRIP_NO" column to the new list items "Trip Id" field (SPFieldNumber)
[Int64]$tn = New-Object System.Int64;
([Int64]::TryParse($item.TRIP_NO, [
] $tn)){
"Trip Id"
] = $tn;
#Add some other text properties
"Flag"
] = $item.FLAG;
"Agent Name"
] = $item.AGENT_NAME;
"Current Location"
] = $item.CURRENT_LOCATION;
#Add user information
"employee"
] = $w.EnsureUser($item.EMPLOYEE); #In
this
case
, the $item.EMPLOYEE value from the spreadsheet
is
a persons name. Eg.
"Matthew Yarlett"
$employeeType.ParseAndSetValue($ni,$item.EMPLOYEE_TYPE); #In
, the $item.EMPLOYEE_TYPE value from the spreadsheet
valid choice present
the EmployeeType list field. Eg.
"Manager"
#Update the item
$ni.Update()
Write-Host ([String]::Format(
"Added record:{0}"
,$r));
$r++;
Richard Mueller edited Revision 1. Comment: Removed extra space in tag "SharePoint 2010"
Matthew Yarlett edited Original. Comment: Updated title.