Friday, August 1, 2014

SharePoint Online: Add New List Item using Client Side Object Model (CSOM) PowerShell

When working with SharePoint Online, had a requirement to add new list items to a list from PowerShell script. Here is my example:

SharePoint online: PowerShell to add list item:

#Load SharePoint CSOM Assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") | Out-Null

#Variables for Processing
$WebUrl = ""
$ListName ="Tasks" 
$AdminName =""
$AdminPassword ="Password1" 

#Get the Client Context
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)

#Login Credentials
$SecurePWD = ConvertTo-SecureString $AdminPassword –asplaintext –force  
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $SecurePWD
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Credential.UserName,$Credential.Password)

$Web = $ctx.Web

#Get the List
$list = $web.get_lists().getByTitle($ListName)

#Add new List Item
$itemCreateInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation 
$listItem = $list.addItem($itemCreateInfo) 

#Set Fields - Internal Name
$listItem.set_item('Title', 'PMO Dashboard Enhancements')  #Task Name

#Set Date Field
$listItem.set_item('StartDate', '01/07/2015')
$listItem.set_item('DueDate', '01/15/2015')

#Set People Picker Field value
$AssignedToUser = $web.EnsureUser("")
$listItem.set_item('AssignedTo', $AssignedToUser)



write-host "New Item has been Added!"  
BTW, You need SharePoint Server 2013 Client Components SDK installed in your machine to use Client side assemblies. Download it from:

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...