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
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"
 
#Variables for Processing
$WebUrl = "https://crescent.sharepoint.com/Sites/Sales"
$ListName ="Tasks" 
$AdminName ="Salaudeen@crescent.OnMicrosoft.com"
$AdminPassword ="Password goes here" 

#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($AdminName,(ConvertTo-SecureString $AdminPassword -AsPlainText -Force))
 
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl) 
$Context.Credentials = $Credentials

#Get the Web and List
$Web = $Context.Web
$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 title

#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("Salaudeen@Salaudeen.OnMicrosoft.com")
$listItem.set_item('AssignedTo', $AssignedToUser)

$listItem.update(); 

#Execute
$Context.Load($listItem) 
$Context.ExecuteQuery()  

write-host "New Item has been Added!"  
Result:
sharepoint online powershell add item to list
BTW, You need SharePoint Server 2013 Client Components SDK installed in your machine to use Client side assemblies. Download it from: http://www.microsoft.com/en-us/download/details.aspx?id=35585



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


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...