Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
PowerShell script connects to a Site Collection within an O365 tenant and does the following:
- Creates a list using the "Custom" list template called "Employee Information"
- Adds a custom site column "LastName"
- Adds an item to the list
All you need to run this script is an O365 tenant and the SharePoint client components SDK installed on the machine running the script: [http://www.microsoft.com/en-us/download/details.aspx?id=35585
](http://www.microsoft.com/en-us/download/details.aspx?id=35585)
#Specify tenant admin and site URL
$User = "girish@wiprooffice15.onmicrosoft.com"
$ListTitle = "Employee Information"
#Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
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"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
#Bind to site collection
write-host $SiteURL
$Context = New-Object Microsoft.SharePoint.Client.ClientContext("https://wiprooffice15.sharepoint.com/sites/Dev")
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds
#Retrieve lists
$Lists = $Context.Web.Lists
$Context.Load($Lists)
$Context.ExecuteQuery()
#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()
$List.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='LastName'/>",$true,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
$List.Update()
$Context.ExecuteQuery()
$csv = Import-CSV .\data.csv
foreach ($row in $csv) {
#Adds an item to the list
$ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$Item = $List.AddItem($ListItemInfo)
$Item["Title"] = $row.Title
$Item["LastName"] = $row.LastName
$Item.Update()
$Context.ExecuteQuery()
}
write-host "done"